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/LAssert.h"
55 #include "support/lyxlib.h"
56 #include "support/os.h"
57 #include "support/path.h"
58 #include "support/textutils.h"
59 #include "support/tostr.h"
61 #include <boost/bind.hpp>
62 #include <boost/tuple/tuple.hpp>
64 #include "support/std_sstream.h"
74 using namespace lyx::support;
84 using std::ostringstream;
93 // all these externs should eventually be removed.
94 extern BufferList bufferlist;
98 const int LYX_FORMAT = 225;
100 bool openFileWrite(ofstream & ofs, string const & fname)
102 ofs.open(fname.c_str());
104 string const file = MakeDisplayPath(fname, 50);
105 string text = bformat(_("Could not open the specified "
106 "document\n%1$s."), file);
107 Alert::error(_("Could not open file"), text);
116 typedef std::map<string, bool> DepClean;
120 Impl(Buffer & parent, string const & file, bool readonly);
122 limited_stack<Undo> undostack;
123 limited_stack<Undo> redostack;
125 ParagraphList paragraphs;
131 /// need to regenerate .tex ?
135 mutable bool lyx_clean;
137 /// is autosave needed
138 mutable bool bak_clean;
140 /// is this a unnamed file (New...)
146 /// name of the file the buffer is associated with.
149 /// The path to the document file.
152 boost::scoped_ptr<Messages> messages;
156 Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
158 lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
159 filename(file), filepath(OnlyPath(file))
161 lyxvc.buffer(&parent);
162 if (readonly_ || lyxrc.use_tempdir)
163 temppath = CreateBufferTmpDir();
167 Buffer::Buffer(string const & file, bool ronly)
168 : pimpl_(new Impl(*this, file, ronly))
170 lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
176 lyxerr[Debug::INFO] << "Buffer::~Buffer()" << endl;
177 // here the buffer should take care that it is
178 // saved properly, before it goes into the void.
182 if (!temppath().empty() && destroyDir(temppath()) != 0) {
183 Alert::warning(_("Could not remove temporary directory"),
184 bformat(_("Could not remove the temporary directory %1$s"), temppath()));
187 paragraphs().clear();
189 // Remove any previewed LaTeX snippets associated with this buffer.
190 lyx::graphics::Previews::get().removeLoader(*this);
194 limited_stack<Undo> & Buffer::undostack()
196 return pimpl_->undostack;
200 limited_stack<Undo> const & Buffer::undostack() const
202 return pimpl_->undostack;
206 limited_stack<Undo> & Buffer::redostack()
208 return pimpl_->redostack;
212 limited_stack<Undo> const & Buffer::redostack() const
214 return pimpl_->redostack;
218 BufferParams & Buffer::params()
220 return pimpl_->params;
224 BufferParams const & Buffer::params() const
226 return pimpl_->params;
230 ParagraphList & Buffer::paragraphs()
232 return pimpl_->paragraphs;
236 ParagraphList const & Buffer::paragraphs() const
238 return pimpl_->paragraphs;
242 LyXVC & Buffer::lyxvc()
244 return pimpl_->lyxvc;
248 LyXVC const & Buffer::lyxvc() const
250 return pimpl_->lyxvc;
254 string const & Buffer::temppath() const
256 return pimpl_->temppath;
260 bool & Buffer::niceFile()
262 return pimpl_->nicefile;
266 bool Buffer::niceFile() const
268 return pimpl_->nicefile;
272 TexRow & Buffer::texrow()
274 return pimpl_->texrow;
278 TexRow const & Buffer::texrow() const
280 return pimpl_->texrow;
284 string const Buffer::getLatexName(bool no_path) const
286 string const name = ChangeExtension(MakeLatexName(fileName()), ".tex");
287 return no_path ? OnlyFilename(name) : name;
291 pair<Buffer::LogType, string> const Buffer::getLogName() const
293 string const filename = getLatexName(false);
295 if (filename.empty())
296 return make_pair(Buffer::latexlog, string());
298 string path = OnlyPath(filename);
300 if (lyxrc.use_tempdir || !IsDirWriteable(path))
303 string const fname = AddName(path,
304 OnlyFilename(ChangeExtension(filename,
307 AddName(path, OnlyFilename(
308 ChangeExtension(filename,
309 formats.extension("literate") + ".out")));
311 // If no Latex log or Build log is newer, show Build log
313 FileInfo const f_fi(fname);
314 FileInfo const b_fi(bname);
317 (!f_fi.exist() || f_fi.getModificationTime() < b_fi.getModificationTime())) {
318 lyxerr[Debug::FILES] << "Log name calculated as: " << bname << endl;
319 return make_pair(Buffer::buildlog, bname);
321 lyxerr[Debug::FILES] << "Log name calculated as: " << fname << endl;
322 return make_pair(Buffer::latexlog, fname);
326 void Buffer::setReadonly(bool flag)
328 if (pimpl_->read_only != flag) {
329 pimpl_->read_only = flag;
335 void Buffer::setFileName(string const & newfile)
337 pimpl_->filename = MakeAbsPath(newfile);
338 pimpl_->filepath = OnlyPath(pimpl_->filename);
339 setReadonly(IsFileWriteable(pimpl_->filename) == 0);
344 // We'll remove this later. (Lgb)
347 void unknownClass(string const & unknown)
349 Alert::warning(_("Unknown document class"),
350 bformat(_("Using the default document class, because the "
351 "class %1$s is unknown."), unknown));
356 int Buffer::readHeader(LyXLex & lex)
358 int unknown_tokens = 0;
362 string const token = lex.getString();
367 if (token == "\\end_header")
370 lyxerr[Debug::PARSER] << "Handling header token: `"
371 << token << '\'' << endl;
374 string unknown = params().readToken(lex, token);
375 if (!unknown.empty()) {
376 if (unknown[0] != '\\') {
377 unknownClass(unknown);
380 string const s = bformat(_("Unknown token: "
384 error(ErrorItem(_("Header error"), s,
389 return unknown_tokens;
393 // candidate for move to BufferView
394 // (at least some parts in the beginning of the func)
397 // changed to be public and have one parameter
398 // if par = 0 normal behavior
399 // else insert behavior
400 // Returns false if "\end_document" is not read (Asger)
401 bool Buffer::readBody(LyXLex & lex, ParagraphList::iterator pit)
403 Paragraph::depth_type depth = 0;
404 bool the_end_read = false;
406 if (paragraphs().empty()) {
408 if (!params().getLyXTextClass().load()) {
409 string theclass = params().getLyXTextClass().name();
410 Alert::error(_("Can't load document class"), bformat(
411 "Using the default document class, because the "
412 " class %1$s could not be loaded.", theclass));
413 params().textclass = 0;
416 // We don't want to adopt the parameters from the
417 // document we insert, so read them into a temporary buffer
418 // and then discard it
420 Buffer tmpbuf("", false);
421 tmpbuf.readHeader(lex);
426 string const token = lex.getString();
431 lyxerr[Debug::PARSER] << "Handling token: `"
432 << token << '\'' << endl;
434 if (token == "\\end_document") {
439 readParagraph(lex, token, paragraphs(), pit, depth);
446 int Buffer::readParagraph(LyXLex & lex, string const & token,
447 ParagraphList & pars, ParagraphList::iterator & pit,
448 Paragraph::depth_type & depth)
450 static Change current_change;
453 if (token == "\\begin_layout") {
454 lex.pushToken(token);
457 par.params().depth(depth);
458 if (params().tracking_changes)
460 LyXFont f(LyXFont::ALL_INHERIT, params().language);
464 if (pit != pars.end())
467 pit = pars.insert(pit, par);
469 // FIXME: goddamn InsetTabular makes us pass a Buffer
471 ::readParagraph(*this, *pit, lex);
473 } else if (token == "\\begin_deeper") {
475 } else if (token == "\\end_deeper") {
477 lex.printError("\\end_deeper: " "depth is already null");
488 // needed to insert the selection
489 void Buffer::insertStringAsLines(ParagraphList::iterator & par, pos_type & pos,
490 LyXFont const & fn,string const & str)
492 LyXLayout_ptr const & layout = par->layout();
496 par->checkInsertChar(font);
497 // insert the string, don't insert doublespace
498 bool space_inserted = true;
499 bool autobreakrows = !par->inInset() ||
500 static_cast<InsetText *>(par->inInset())->getAutoBreakRows();
501 for(string::const_iterator cit = str.begin();
502 cit != str.end(); ++cit) {
504 if (autobreakrows && (!par->empty() || par->allowEmpty())) {
505 breakParagraph(params(), paragraphs(), par, pos,
506 layout->isEnvironment());
509 space_inserted = true;
513 // do not insert consecutive spaces if !free_spacing
514 } else if ((*cit == ' ' || *cit == '\t') &&
515 space_inserted && !par->isFreeSpacing()) {
517 } else if (*cit == '\t') {
518 if (!par->isFreeSpacing()) {
519 // tabs are like spaces here
520 par->insertChar(pos, ' ', font);
522 space_inserted = true;
524 const pos_type nb = 8 - pos % 8;
525 for (pos_type a = 0; a < nb ; ++a) {
526 par->insertChar(pos, ' ', font);
529 space_inserted = true;
531 } else if (!IsPrintable(*cit)) {
532 // Ignore unprintables
535 // just insert the character
536 par->insertChar(pos, *cit, font);
538 space_inserted = (*cit == ' ');
545 bool Buffer::readFile(string const & filename)
547 // Check if the file is compressed.
548 string const format = getExtFromContents(filename);
549 if (format == "gzip" || format == "zip" || format == "compress") {
550 params().compressed = true;
553 bool ret = readFile(filename, paragraphs().begin());
555 // After we have read a file, we must ensure that the buffer
556 // language is set and used in the gui.
557 // If you know of a better place to put this, please tell me. (Lgb)
558 updateDocLang(params().language);
564 bool Buffer::readFile(string const & filename, ParagraphList::iterator pit)
567 lex.setFile(filename);
569 return readFile(lex, filename, pit);
573 bool Buffer::readFile(LyXLex & lex, string const & filename,
574 ParagraphList::iterator pit)
576 Assert(!filename.empty());
579 Alert::error(_("Document could not be read"),
580 bformat(_("%1$s could not be read."), filename));
585 string const token(lex.getString());
588 Alert::error(_("Document could not be read"),
589 bformat(_("%1$s could not be read."), filename));
593 // the first token _must_ be...
594 if (token != "\\lyxformat") {
595 lyxerr << "Token: " << token << endl;
597 Alert::error(_("Document format failure"),
598 bformat(_("%1$s is not a LyX document."),
604 string tmp_format = lex.getString();
605 //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
606 // if present remove ".," from string.
607 string::size_type dot = tmp_format.find_first_of(".,");
608 //lyxerr << " dot found at " << dot << endl;
609 if (dot != string::npos)
610 tmp_format.erase(dot, 1);
611 int file_format = strToInt(tmp_format);
612 //lyxerr << "format: " << file_format << endl;
614 if (file_format > LYX_FORMAT) {
615 Alert::warning(_("Document format failure"),
616 bformat(_("%1$s was created with a newer"
617 " version of LyX. This is likely to"
620 } else if (file_format < LYX_FORMAT) {
621 string const tmpfile = tempName();
622 string command = LibFileSearch("lyx2lyx", "lyx2lyx");
623 if (command.empty()) {
624 Alert::error(_("Conversion script not found"),
625 bformat(_("%1$s is from an earlier"
626 " version of LyX, but the"
627 " conversion script lyx2lyx"
628 " could not be found."),
634 + " -o " + tmpfile + ' '
635 + QuoteName(filename);
636 lyxerr[Debug::INFO] << "Running '"
639 cmd_ret const ret = RunCommand(command);
640 if (ret.first != 0) {
641 Alert::error(_("Conversion script failed"),
642 bformat(_("%1$s is from an earlier version"
643 " of LyX, but the lyx2lyx script"
644 " failed to convert it."),
648 bool ret = readFile(tmpfile, pit);
649 // Do stuff with tmpfile name and buffer name here.
655 bool the_end = readBody(lex, pit);
656 params().setPaperStuff();
659 Alert::error(_("Document format failure"),
660 bformat(_("%1$s ended unexpectedly, which means"
661 " that it is probably corrupted."),
668 // Should probably be moved to somewhere else: BufferView? LyXView?
669 bool Buffer::save() const
671 // We don't need autosaves in the immediate future. (Asger)
672 resetAutosaveTimers();
676 if (lyxrc.make_backup) {
677 s = fileName() + '~';
678 if (!lyxrc.backupdir_path.empty())
679 s = AddName(lyxrc.backupdir_path,
680 subst(os::slashify_path(s),'/','!'));
682 // Rename is the wrong way of making a backup,
683 // this is the correct way.
684 /* truss cp fil fil2:
685 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
686 stat("LyXVC.lyx", 0xEFFFF688) = 0
687 open("LyXVC.lyx", O_RDONLY) = 3
688 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
689 fstat(4, 0xEFFFF508) = 0
690 fstat(3, 0xEFFFF508) = 0
691 read(3, " # T h i s f i l e w".., 8192) = 5579
692 write(4, " # T h i s f i l e w".., 5579) = 5579
693 read(3, 0xEFFFD4A0, 8192) = 0
696 chmod("LyXVC3.lyx", 0100644) = 0
697 lseek(0, 0, SEEK_CUR) = 46440
701 // Should probably have some more error checking here.
702 // Doing it this way, also makes the inodes stay the same.
703 // This is still not a very good solution, in particular we
704 // might loose the owner of the backup.
705 FileInfo finfo(fileName());
707 mode_t fmode = finfo.getMode();
708 struct utimbuf times = {
709 finfo.getAccessTime(),
710 finfo.getModificationTime() };
712 ifstream ifs(fileName().c_str());
713 ofstream ofs(s.c_str(), ios::out|ios::trunc);
718 ::chmod(s.c_str(), fmode);
720 if (::utime(s.c_str(), ×)) {
721 lyxerr << "utime error." << endl;
724 lyxerr << "LyX was not able to make "
725 "backup copy. Beware." << endl;
730 if (writeFile(fileName())) {
732 removeAutosaveFile(fileName());
734 // Saving failed, so backup is not backup
735 if (lyxrc.make_backup) {
736 rename(s, fileName());
744 bool Buffer::writeFile(string const & fname) const
746 if (pimpl_->read_only && (fname == fileName())) {
750 FileInfo finfo(fname);
751 if (finfo.exist() && !finfo.writable()) {
757 if (params().compressed) {
758 gz::ogzstream ofs(fname.c_str());
763 retval = do_writeFile(ofs);
766 ofstream ofs(fname.c_str());
770 retval = do_writeFile(ofs);
777 bool Buffer::do_writeFile(ostream & ofs) const
781 // Use the standard "C" locale for file output.
782 ofs.imbue(std::locale::classic());
785 // The top of the file should not be written by params().
787 // write out a comment in the top of the file
788 ofs << "#LyX " << lyx_version
789 << " created this file. For more info see http://www.lyx.org/\n"
790 << "\\lyxformat " << LYX_FORMAT << "\n";
792 // now write out the buffer paramters.
793 params().writeFile(ofs);
795 ofs << "\\end_header\n";
797 Paragraph::depth_type depth = 0;
799 // this will write out all the paragraphs
800 // using recursive descent.
801 ParagraphList::const_iterator pit = paragraphs().begin();
802 ParagraphList::const_iterator pend = paragraphs().end();
803 for (; pit != pend; ++pit)
804 pit->write(*this, ofs, params(), depth);
806 // Write marker that shows file is complete
807 ofs << "\n\\end_document" << endl;
809 // Shouldn't really be needed....
812 // how to check if close went ok?
813 // Following is an attempt... (BE 20001011)
815 // good() returns false if any error occured, including some
817 // bad() returns true if something bad happened in the buffer,
818 // which should include file system full errors.
825 lyxerr << "Buffer::writeFile: BAD ERROR!" << endl;
827 lyxerr << "Buffer::writeFile: NOT SO BAD ERROR!"
839 pair<int, string> const addDepth(int depth, int ldepth)
843 d += (ldepth - depth) * 2;
844 return make_pair(d, string(d, ' '));
850 string const Buffer::asciiParagraph(Paragraph const & par,
851 unsigned int linelen,
852 bool noparbreak) const
854 ostringstream buffer;
856 Paragraph::depth_type ltype_depth = 0;
857 bool ref_printed = false;
858 Paragraph::depth_type depth = par.params().depth();
860 // First write the layout
861 string const & tmp = par.layout()->name();
862 if (compare_no_case(tmp, "itemize") == 0) {
864 ltype_depth = depth + 1;
865 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
867 ltype_depth = depth + 1;
868 } else if (contains(ascii_lowercase(tmp), "ection")) {
870 ltype_depth = depth + 1;
871 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
873 ltype_depth = depth + 1;
874 } else if (compare_ascii_no_case(tmp, "description") == 0) {
876 ltype_depth = depth + 1;
877 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
880 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
888 /* maybe some vertical spaces */
890 /* the labelwidthstring used in lists */
894 /* some pagebreaks? */
898 /* what about the alignment */
900 // linelen <= 0 is special and means we don't have paragraph breaks
902 string::size_type currlinelen = 0;
908 buffer << string(depth * 2, ' ');
909 currlinelen += depth * 2;
912 // we should probably change to the paragraph language in the
913 // gettext here (if possible) so that strings are outputted in
914 // the correct language! (20012712 Jug)
918 case 4: // (Sub)Paragraph
919 case 5: // Description
923 buffer << _("Abstract") << "\n\n";
926 string const abst = _("Abstract: ");
928 currlinelen += abst.length();
931 case 7: // Bibliography
934 buffer << _("References") << "\n\n";
937 string const refs = _("References: ");
939 currlinelen += refs.length();
947 string const parlab = par.params().labelString();
948 buffer << parlab << ' ';
949 currlinelen += parlab.length() + 1;
957 pair<int, string> p = addDepth(depth, ltype_depth);
959 currlinelen += p.first;
962 // this is to change the linebreak to do it by word a bit more
963 // intelligent hopefully! (only in the case where we have a
964 // max linelength!) (Jug)
968 for (pos_type i = 0; i < par.size(); ++i) {
969 char c = par.getUChar(params(), i);
971 case Paragraph::META_INSET:
973 InsetOld const * inset = par.getInset(i);
977 currlinelen += word.length();
980 if (inset->ascii(*this, buffer, linelen)) {
981 // to be sure it breaks paragraph
982 currlinelen += linelen;
991 currlinelen + word.length() > linelen - 10) {
993 pair<int, string> p = addDepth(depth, ltype_depth);
995 currlinelen = p.first;
998 buffer << word << ' ';
999 currlinelen += word.length() + 1;
1006 lyxerr[Debug::INFO] <<
1007 "writeAsciiFile: NULL char in structure." << endl;
1009 if ((linelen > 0) &&
1010 (currlinelen + word.length()) > linelen)
1014 pair<int, string> p =
1015 addDepth(depth, ltype_depth);
1017 currlinelen = p.first;
1024 return STRCONV(buffer.str());
1028 void Buffer::writeFileAscii(string const & fname, int linelen)
1031 if (!::openFileWrite(ofs, fname))
1033 writeFileAscii(ofs, linelen);
1037 void Buffer::writeFileAscii(ostream & os, int linelen)
1039 ParagraphList::iterator beg = paragraphs().begin();
1040 ParagraphList::iterator end = paragraphs().end();
1041 ParagraphList::iterator it = beg;
1042 for (; it != end; ++it) {
1043 os << asciiParagraph(*it, linelen, it == beg);
1049 void Buffer::makeLaTeXFile(string const & fname,
1050 string const & original_path,
1051 LatexRunParams const & runparams,
1052 bool output_preamble, bool output_body)
1054 lyxerr[Debug::LATEX] << "makeLaTeXFile..." << endl;
1057 if (!::openFileWrite(ofs, fname))
1060 makeLaTeXFile(ofs, original_path,
1061 runparams, output_preamble, output_body);
1065 lyxerr << "File was not closed properly." << endl;
1070 void Buffer::makeLaTeXFile(ostream & os,
1071 string const & original_path,
1072 LatexRunParams const & runparams_in,
1073 bool output_preamble, bool output_body)
1075 LatexRunParams runparams = runparams_in;
1076 niceFile() = runparams.nice; // this will be used by Insetincludes.
1078 // validate the buffer.
1079 lyxerr[Debug::LATEX] << " Validating buffer..." << endl;
1080 LaTeXFeatures features(params());
1082 lyxerr[Debug::LATEX] << " Buffer validation done." << endl;
1085 // The starting paragraph of the coming rows is the
1086 // first paragraph of the document. (Asger)
1087 texrow().start(paragraphs().begin()->id(), 0);
1089 if (output_preamble && runparams.nice) {
1090 os << "%% LyX " << lyx_version << " created this file. "
1091 "For more info, see http://www.lyx.org/.\n"
1092 "%% Do not edit unless you really know what "
1097 lyxerr[Debug::INFO] << "lyx header finished" << endl;
1098 // There are a few differences between nice LaTeX and usual files:
1099 // usual is \batchmode and has a
1100 // special input@path to allow the including of figures
1101 // with either \input or \includegraphics (what figinsets do).
1102 // input@path is set when the actual parameter
1103 // original_path is set. This is done for usual tex-file, but not
1104 // for nice-latex-file. (Matthias 250696)
1105 if (output_preamble) {
1106 if (!runparams.nice) {
1107 // code for usual, NOT nice-latex-file
1108 os << "\\batchmode\n"; // changed
1109 // from \nonstopmode
1112 if (!original_path.empty()) {
1113 string inputpath = os::external_path(original_path);
1114 subst(inputpath, "~", "\\string~");
1115 os << "\\makeatletter\n"
1116 << "\\def\\input@path{{"
1117 << inputpath << "/}}\n"
1118 << "\\makeatother\n";
1124 // Write the preamble
1125 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1131 os << "\\begin{document}\n";
1133 } // output_preamble
1134 lyxerr[Debug::INFO] << "preamble finished, now the body." << endl;
1136 if (!lyxrc.language_auto_begin) {
1137 os << subst(lyxrc.language_command_begin, "$$lang",
1138 params().language->babel())
1143 latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1145 // add this just in case after all the paragraphs
1149 if (!lyxrc.language_auto_end) {
1150 os << subst(lyxrc.language_command_end, "$$lang",
1151 params().language->babel())
1156 if (output_preamble) {
1157 os << "\\end{document}\n";
1160 lyxerr[Debug::LATEX] << "makeLaTeXFile...done" << endl;
1162 lyxerr[Debug::LATEX] << "LaTeXFile for inclusion made."
1166 // Just to be sure. (Asger)
1169 lyxerr[Debug::INFO] << "Finished making LaTeX file." << endl;
1170 lyxerr[Debug::INFO] << "Row count was " << texrow().rows() - 1
1173 // we want this to be true outside previews (for insetexternal)
1178 bool Buffer::isLatex() const
1180 return params().getLyXTextClass().outputType() == LATEX;
1184 bool Buffer::isLinuxDoc() const
1186 return params().getLyXTextClass().outputType() == LINUXDOC;
1190 bool Buffer::isLiterate() const
1192 return params().getLyXTextClass().outputType() == LITERATE;
1196 bool Buffer::isDocBook() const
1198 return params().getLyXTextClass().outputType() == DOCBOOK;
1202 bool Buffer::isSGML() const
1204 LyXTextClass const & tclass = params().getLyXTextClass();
1206 return tclass.outputType() == LINUXDOC ||
1207 tclass.outputType() == DOCBOOK;
1211 void Buffer::makeLinuxDocFile(string const & fname, bool nice, bool body_only)
1214 if (!::openFileWrite(ofs, fname))
1217 niceFile() = nice; // this will be used by included files.
1219 LaTeXFeatures features(params());
1225 LyXTextClass const & tclass = params().getLyXTextClass();
1227 string top_element = tclass.latexname();
1230 ofs << "<!doctype linuxdoc system";
1232 string preamble = params().preamble;
1233 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1235 preamble += features.getIncludedFiles(name);
1236 preamble += features.getLyXSGMLEntities();
1238 if (!preamble.empty()) {
1239 ofs << " [ " << preamble << " ]";
1243 if (params().options.empty())
1244 sgml::openTag(ofs, 0, false, top_element);
1246 string top = top_element;
1248 top += params().options;
1249 sgml::openTag(ofs, 0, false, top);
1253 ofs << "<!-- LyX " << lyx_version
1254 << " created this file. For more info see http://www.lyx.org/"
1257 Paragraph::depth_type depth = 0; // paragraph depth
1259 vector<string> environment_stack(5);
1261 ParagraphList::iterator pit = paragraphs().begin();
1262 ParagraphList::iterator pend = paragraphs().end();
1263 for (; pit != pend; ++pit) {
1264 LyXLayout_ptr const & style = pit->layout();
1265 // treat <toc> as a special case for compatibility with old code
1266 if (pit->isInset(0)) {
1267 InsetOld * inset = pit->getInset(0);
1268 InsetOld::Code lyx_code = inset->lyxCode();
1269 if (lyx_code == InsetOld::TOC_CODE) {
1270 string const temp = "toc";
1271 sgml::openTag(ofs, depth, false, temp);
1276 // environment tag closing
1277 for (; depth > pit->params().depth(); --depth) {
1278 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1279 environment_stack[depth].erase();
1282 // write opening SGML tags
1283 switch (style->latextype) {
1284 case LATEX_PARAGRAPH:
1285 if (depth == pit->params().depth()
1286 && !environment_stack[depth].empty()) {
1287 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1288 environment_stack[depth].erase();
1294 sgml::openTag(ofs, depth, false, style->latexname());
1299 error(ErrorItem(_("Error:"), _("Wrong depth for LatexType Command.\n"), pit->id(), 0, pit->size()));
1301 if (!environment_stack[depth].empty()) {
1302 sgml::closeTag(ofs, depth, false, environment_stack[depth]);
1306 environment_stack[depth].erase();
1307 sgml::openTag(ofs, depth, false, style->latexname());
1310 case LATEX_ENVIRONMENT:
1311 case LATEX_ITEM_ENVIRONMENT:
1312 case LATEX_BIB_ENVIRONMENT:
1314 string const & latexname = style->latexname();
1316 if (depth == pit->params().depth()
1317 && environment_stack[depth] != latexname) {
1318 sgml::closeTag(ofs, depth, false,
1319 environment_stack[depth]);
1320 environment_stack[depth].erase();
1322 if (depth < pit->params().depth()) {
1323 depth = pit->params().depth();
1324 environment_stack[depth].erase();
1326 if (environment_stack[depth] != latexname) {
1328 sgml::openTag(ofs, depth, false, "p");
1330 sgml::openTag(ofs, depth, false, latexname);
1332 if (environment_stack.size() == depth + 1)
1333 environment_stack.push_back("!-- --");
1334 environment_stack[depth] = latexname;
1337 if (style->latexparam() == "CDATA")
1340 if (style->latextype == LATEX_ENVIRONMENT) break;
1342 if (style->labeltype == LABEL_MANUAL)
1347 sgml::openTag(ofs, depth + 1, false, item_name);
1352 sgml::openTag(ofs, depth, false, style->latexname());
1356 simpleLinuxDocOnePar(ofs, pit, depth);
1359 // write closing SGML tags
1360 switch (style->latextype) {
1363 case LATEX_ENVIRONMENT:
1364 case LATEX_ITEM_ENVIRONMENT:
1365 case LATEX_BIB_ENVIRONMENT:
1366 if (style->latexparam() == "CDATA")
1370 sgml::closeTag(ofs, depth, false, style->latexname());
1376 for (int i = depth; i >= 0; --i)
1377 sgml::closeTag(ofs, depth, false, environment_stack[i]);
1381 sgml::closeTag(ofs, 0, false, top_element);
1385 // How to check for successful close
1387 // we want this to be true outside previews (for insetexternal)
1392 // checks, if newcol chars should be put into this line
1393 // writes newline, if necessary.
1396 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1397 string::size_type newcol)
1400 if (colcount > lyxrc.ascii_linelen) {
1402 colcount = newcol; // assume write after this call
1417 string tag_name(PAR_TAG const & pt) {
1419 case NONE: return "!-- --";
1420 case TT: return "tt";
1421 case SF: return "sf";
1422 case BF: return "bf";
1423 case IT: return "it";
1424 case SL: return "sl";
1425 case EM: return "em";
1432 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1434 p1 = static_cast<PAR_TAG>(p1 | p2);
1439 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1441 p1 = static_cast<PAR_TAG>(p1 & ~p2);
1447 // Handle internal paragraph parsing -- layout already processed.
1448 void Buffer::simpleLinuxDocOnePar(ostream & os,
1449 ParagraphList::iterator par,
1450 Paragraph::depth_type /*depth*/) const
1452 LyXLayout_ptr const & style = par->layout();
1454 string::size_type char_line_count = 5; // Heuristic choice ;-)
1456 // gets paragraph main font
1459 if (style->labeltype == LABEL_MANUAL) {
1460 font_old = style->labelfont;
1463 font_old = style->font;
1467 LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1468 LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1469 LyXFont::FONT_SHAPE shape_type = LyXFont::UP_SHAPE;
1472 stack<PAR_TAG> tag_state;
1473 // parsing main loop
1474 for (pos_type i = 0; i < par->size(); ++i) {
1476 PAR_TAG tag_close = NONE;
1477 list < PAR_TAG > tag_open;
1479 LyXFont const font = par->getFont(params(), i, outerFont(par, paragraphs()));
1481 if (font_old.family() != font.family()) {
1482 switch (family_type) {
1483 case LyXFont::SANS_FAMILY:
1486 case LyXFont::TYPEWRITER_FAMILY:
1493 family_type = font.family();
1495 switch (family_type) {
1496 case LyXFont::SANS_FAMILY:
1497 tag_open.push_back(SF);
1499 case LyXFont::TYPEWRITER_FAMILY:
1500 tag_open.push_back(TT);
1507 if (font_old.series() != font.series()) {
1508 switch (series_type) {
1509 case LyXFont::BOLD_SERIES:
1516 series_type = font.series();
1518 switch (series_type) {
1519 case LyXFont::BOLD_SERIES:
1520 tag_open.push_back(BF);
1528 if (font_old.shape() != font.shape()) {
1529 switch (shape_type) {
1530 case LyXFont::ITALIC_SHAPE:
1533 case LyXFont::SLANTED_SHAPE:
1540 shape_type = font.shape();
1542 switch (shape_type) {
1543 case LyXFont::ITALIC_SHAPE:
1544 tag_open.push_back(IT);
1546 case LyXFont::SLANTED_SHAPE:
1547 tag_open.push_back(SL);
1554 if (font_old.emph() != font.emph()) {
1555 if (font.emph() == LyXFont::ON) {
1556 tag_open.push_back(EM);
1565 list < PAR_TAG > temp;
1566 while (!tag_state.empty() && tag_close) {
1567 PAR_TAG k = tag_state.top();
1569 os << "</" << tag_name(k) << '>';
1576 for(list< PAR_TAG >::const_iterator j = temp.begin();
1577 j != temp.end(); ++j) {
1579 os << '<' << tag_name(*j) << '>';
1582 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1583 j != tag_open.end(); ++j) {
1585 os << '<' << tag_name(*j) << '>';
1588 char c = par->getChar(i);
1590 if (c == Paragraph::META_INSET) {
1591 InsetOld * inset = par->getInset(i);
1592 inset->linuxdoc(*this, os);
1597 if (style->latexparam() == "CDATA") {
1598 // "TeX"-Mode on == > SGML-Mode on.
1605 boost::tie(ws, str) = sgml::escapeChar(c);
1606 if (ws && !par->isFreeSpacing()) {
1607 // in freespacing mode, spaces are
1608 // non-breaking characters
1609 if (desc_on) {// if char is ' ' then...
1612 sgmlLineBreak(os, char_line_count, 6);
1616 sgmlLineBreak(os, char_line_count, 1);
1621 char_line_count += str.length();
1627 while (!tag_state.empty()) {
1628 os << "</" << tag_name(tag_state.top()) << '>';
1632 // resets description flag correctly
1634 // <tag> not closed...
1635 sgmlLineBreak(os, char_line_count, 6);
1641 void Buffer::makeDocBookFile(string const & fname, bool nice, bool only_body)
1644 if (!::openFileWrite(ofs, fname))
1647 niceFile() = nice; // this will be used by Insetincludes.
1649 LaTeXFeatures features(params());
1654 LyXTextClass const & tclass = params().getLyXTextClass();
1655 string top_element = tclass.latexname();
1658 ofs << "<!DOCTYPE " << top_element
1659 << " PUBLIC \"-//OASIS//DTD DocBook V4.1//EN\"";
1661 string preamble = params().preamble;
1662 string const name = nice ? ChangeExtension(pimpl_->filename, ".sgml")
1664 preamble += features.getIncludedFiles(name);
1665 preamble += features.getLyXSGMLEntities();
1667 if (!preamble.empty()) {
1668 ofs << "\n [ " << preamble << " ]";
1673 string top = top_element;
1675 top += params().language->code();
1678 if (!params().options.empty()) {
1680 top += params().options;
1682 sgml::openTag(ofs, 0, false, top);
1684 ofs << "<!-- DocBook file was created by LyX " << lyx_version
1685 << "\n See http://www.lyx.org/ for more information -->\n";
1687 vector<string> environment_stack(10);
1688 vector<string> environment_inner(10);
1689 vector<string> command_stack(10);
1691 bool command_flag = false;
1692 Paragraph::depth_type command_depth = 0;
1693 Paragraph::depth_type command_base = 0;
1694 Paragraph::depth_type cmd_depth = 0;
1695 Paragraph::depth_type depth = 0; // paragraph depth
1698 string command_name;
1700 ParagraphList::iterator par = paragraphs().begin();
1701 ParagraphList::iterator pend = paragraphs().end();
1703 for (; par != pend; ++par) {
1707 int desc_on = 0; // description mode
1709 LyXLayout_ptr const & style = par->layout();
1711 // environment tag closing
1712 for (; depth > par->params().depth(); --depth) {
1713 if (environment_inner[depth] != "!-- --" && !environment_inner[depth].empty()) {
1714 item_name = "listitem";
1715 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1716 if (environment_inner[depth] == "varlistentry")
1717 sgml::closeTag(ofs, depth+command_depth, false, environment_inner[depth]);
1719 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1720 environment_stack[depth].erase();
1721 environment_inner[depth].erase();
1724 if (depth == par->params().depth()
1725 && environment_stack[depth] != style->latexname()
1726 && !environment_stack[depth].empty()) {
1727 if (environment_inner[depth] != "!-- --") {
1728 item_name= "listitem";
1729 sgml::closeTag(ofs, command_depth+depth, false, item_name);
1730 if (environment_inner[depth] == "varlistentry")
1731 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1734 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1736 environment_stack[depth].erase();
1737 environment_inner[depth].erase();
1740 // Write opening SGML tags.
1741 switch (style->latextype) {
1742 case LATEX_PARAGRAPH:
1743 sgml::openTag(ofs, depth + command_depth,
1744 false, style->latexname());
1749 error(ErrorItem(_("Error"), _("Wrong depth for LatexType Command."), par->id(), 0, par->size()));
1751 command_name = style->latexname();
1753 sgmlparam = style->latexparam();
1754 c_params = split(sgmlparam, c_depth,'|');
1756 cmd_depth = atoi(c_depth);
1759 if (cmd_depth < command_base) {
1760 for (Paragraph::depth_type j = command_depth;
1761 j >= command_base; --j) {
1762 sgml::closeTag(ofs, j, false, command_stack[j]);
1765 command_depth = command_base = cmd_depth;
1766 } else if (cmd_depth <= command_depth) {
1767 for (int j = command_depth;
1768 j >= int(cmd_depth); --j) {
1769 sgml::closeTag(ofs, j, false, command_stack[j]);
1772 command_depth = cmd_depth;
1774 command_depth = cmd_depth;
1776 command_depth = command_base = cmd_depth;
1777 command_flag = true;
1779 if (command_stack.size() == command_depth + 1)
1780 command_stack.push_back(string());
1781 command_stack[command_depth] = command_name;
1783 // treat label as a special case for
1784 // more WYSIWYM handling.
1785 // This is a hack while paragraphs can't have
1786 // attributes, like id in this case.
1787 if (par->isInset(0)) {
1788 InsetOld * inset = par->getInset(0);
1789 InsetOld::Code lyx_code = inset->lyxCode();
1790 if (lyx_code == InsetOld::LABEL_CODE) {
1791 command_name += " id=\"";
1792 command_name += (static_cast<InsetCommand *>(inset))->getContents();
1793 command_name += '"';
1798 sgml::openTag(ofs, depth + command_depth, false, command_name);
1800 item_name = c_params.empty() ? "title" : c_params;
1801 sgml::openTag(ofs, depth + 1 + command_depth, false, item_name);
1804 case LATEX_ENVIRONMENT:
1805 case LATEX_ITEM_ENVIRONMENT:
1806 if (depth < par->params().depth()) {
1807 depth = par->params().depth();
1808 environment_stack[depth].erase();
1811 if (environment_stack[depth] != style->latexname()) {
1812 if (environment_stack.size() == depth + 1) {
1813 environment_stack.push_back("!-- --");
1814 environment_inner.push_back("!-- --");
1816 environment_stack[depth] = style->latexname();
1817 environment_inner[depth] = "!-- --";
1818 sgml::openTag(ofs, depth + command_depth, false, environment_stack[depth]);
1820 if (environment_inner[depth] != "!-- --") {
1821 item_name= "listitem";
1822 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1823 if (environment_inner[depth] == "varlistentry")
1824 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1828 if (style->latextype == LATEX_ENVIRONMENT) {
1829 if (!style->latexparam().empty()) {
1830 if (style->latexparam() == "CDATA")
1833 sgml::openTag(ofs, depth + command_depth, false, style->latexparam());
1838 desc_on = (style->labeltype == LABEL_MANUAL);
1840 environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1841 sgml::openTag(ofs, depth + 1 + command_depth,
1842 false, environment_inner[depth]);
1844 item_name = desc_on ? "term" : "para";
1845 sgml::openTag(ofs, depth + 1 + command_depth,
1849 sgml::openTag(ofs, depth + command_depth,
1850 false, style->latexname());
1854 simpleDocBookOnePar(ofs, par, desc_on,
1855 depth + 1 + command_depth);
1858 // write closing SGML tags
1859 switch (style->latextype) {
1861 end_tag = c_params.empty() ? "title" : c_params;
1862 sgml::closeTag(ofs, depth + command_depth,
1865 case LATEX_ENVIRONMENT:
1866 if (!style->latexparam().empty()) {
1867 if (style->latexparam() == "CDATA")
1870 sgml::closeTag(ofs, depth + command_depth, false, style->latexparam());
1873 case LATEX_ITEM_ENVIRONMENT:
1874 if (desc_on == 1) break;
1876 sgml::closeTag(ofs, depth + 1 + command_depth, false, end_tag);
1878 case LATEX_PARAGRAPH:
1879 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1882 sgml::closeTag(ofs, depth + command_depth, false, style->latexname());
1888 for (int d = depth; d >= 0; --d) {
1889 if (!environment_stack[depth].empty()) {
1890 if (environment_inner[depth] != "!-- --") {
1891 item_name = "listitem";
1892 sgml::closeTag(ofs, command_depth + depth, false, item_name);
1893 if (environment_inner[depth] == "varlistentry")
1894 sgml::closeTag(ofs, depth + command_depth, false, environment_inner[depth]);
1897 sgml::closeTag(ofs, depth + command_depth, false, environment_stack[depth]);
1901 for (int j = command_depth; j >= 0 ; --j)
1902 if (!command_stack[j].empty()) {
1903 sgml::closeTag(ofs, j, false, command_stack[j]);
1908 sgml::closeTag(ofs, 0, false, top_element);
1911 // How to check for successful close
1913 // we want this to be true outside previews (for insetexternal)
1918 void Buffer::simpleDocBookOnePar(ostream & os,
1919 ParagraphList::iterator par, int & desc_on,
1920 Paragraph::depth_type depth) const
1922 bool emph_flag = false;
1924 LyXLayout_ptr const & style = par->layout();
1926 LyXFont font_old = (style->labeltype == LABEL_MANUAL ? style->labelfont : style->font);
1928 int char_line_count = depth;
1929 //if (!style.free_spacing)
1930 // os << string(depth,' ');
1932 // parsing main loop
1933 for (pos_type i = 0; i < par->size(); ++i) {
1934 LyXFont font = par->getFont(params(), i, outerFont(par, paragraphs()));
1936 // handle <emphasis> tag
1937 if (font_old.emph() != font.emph()) {
1938 if (font.emph() == LyXFont::ON) {
1939 if (style->latexparam() == "CDATA")
1942 if (style->latexparam() == "CDATA")
1946 if (style->latexparam() == "CDATA")
1948 os << "</emphasis>";
1949 if (style->latexparam() == "CDATA")
1956 if (par->isInset(i)) {
1957 InsetOld * inset = par->getInset(i);
1958 // don't print the inset in position 0 if desc_on == 3 (label)
1959 if (i || desc_on != 3) {
1960 if (style->latexparam() == "CDATA")
1962 inset->docbook(*this, os, false);
1963 if (style->latexparam() == "CDATA")
1967 char c = par->getChar(i);
1970 boost::tie(ws, str) = sgml::escapeChar(c);
1972 if (style->pass_thru) {
1974 } else if (par->isFreeSpacing() || c != ' ') {
1976 } else if (desc_on == 1) {
1978 os << "\n</term><listitem><para>";
1988 if (style->latexparam() == "CDATA")
1990 os << "</emphasis>";
1991 if (style->latexparam() == "CDATA")
1995 // resets description flag correctly
1997 // <term> not closed...
1998 os << "</term>\n<listitem><para> </para>";
2000 if (style->free_spacing)
2005 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
2006 // Other flags: -wall -v0 -x
2007 int Buffer::runChktex()
2011 // get LaTeX-Filename
2012 string const name = getLatexName();
2013 string path = filePath();
2015 string const org_path = path;
2016 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
2020 Path p(path); // path to LaTeX file
2021 message(_("Running chktex..."));
2023 // Generate the LaTeX file if neccessary
2024 LatexRunParams runparams;
2025 runparams.flavor = LatexRunParams::LATEX;
2026 runparams.nice = false;
2027 makeLaTeXFile(name, org_path, runparams);
2030 Chktex chktex(lyxrc.chktex_command, name, filePath());
2031 int res = chktex.run(terr); // run chktex
2034 Alert::error(_("chktex failure"),
2035 _("Could not run chktex successfully."));
2036 } else if (res > 0) {
2037 // Insert all errors as errors boxes
2038 bufferErrors(*this, terr);
2047 void Buffer::validate(LaTeXFeatures & features) const
2049 LyXTextClass const & tclass = params().getLyXTextClass();
2051 if (params().tracking_changes) {
2052 features.require("dvipost");
2053 features.require("color");
2056 // AMS Style is at document level
2057 if (params().use_amsmath == BufferParams::AMS_ON
2058 || tclass.provides(LyXTextClass::amsmath))
2059 features.require("amsmath");
2061 for_each(paragraphs().begin(), paragraphs().end(),
2062 boost::bind(&Paragraph::validate, _1, boost::ref(features)));
2064 // the bullet shapes are buffer level not paragraph level
2065 // so they are tested here
2066 for (int i = 0; i < 4; ++i) {
2067 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
2068 int const font = params().user_defined_bullet(i).getFont();
2070 int const c = params()
2071 .user_defined_bullet(i)
2078 features.require("latexsym");
2080 } else if (font == 1) {
2081 features.require("amssymb");
2082 } else if ((font >= 2 && font <= 5)) {
2083 features.require("pifont");
2088 if (lyxerr.debugging(Debug::LATEX)) {
2089 features.showStruct();
2094 void Buffer::getLabelList(std::vector<string> & list) const
2096 /// if this is a child document and the parent is already loaded
2097 /// Use the parent's list instead [ale990407]
2098 if (!params().parentname.empty()
2099 && bufferlist.exists(params().parentname)) {
2100 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2102 tmp->getLabelList(list);
2107 for (inset_iterator it = inset_const_iterator_begin();
2108 it != inset_const_iterator_end(); ++it) {
2109 it->getLabelList(list);
2114 // This is also a buffer property (ale)
2115 void Buffer::fillWithBibKeys(std::vector<std::pair<string, string> > & keys) const
2117 /// if this is a child document and the parent is already loaded
2118 /// use the parent's list instead [ale990412]
2119 if (!params().parentname.empty() &&
2120 bufferlist.exists(params().parentname)) {
2121 Buffer const * tmp = bufferlist.getBuffer(params().parentname);
2123 tmp->fillWithBibKeys(keys);
2128 for (inset_iterator it = inset_const_iterator_begin();
2129 it != inset_const_iterator_end(); ++it) {
2130 if (it->lyxCode() == InsetOld::BIBTEX_CODE)
2131 static_cast<InsetBibtex &>(*it).fillWithBibKeys(*this, keys);
2132 else if (it->lyxCode() == InsetOld::INCLUDE_CODE)
2133 static_cast<InsetInclude &>(*it).fillWithBibKeys(keys);
2134 else if (it->lyxCode() == InsetOld::BIBITEM_CODE) {
2135 InsetBibitem & bib = static_cast<InsetBibitem &>(*it);
2136 string const key = bib.getContents();
2137 string const opt = bib.getOptions();
2138 string const ref; // = pit->asString(this, false);
2139 string const info = opt + "TheBibliographyRef" + ref;
2140 keys.push_back(pair<string, string>(key, info));
2146 bool Buffer::isDepClean(string const & name) const
2148 DepClean::const_iterator it = pimpl_->dep_clean.find(name);
2149 if (it == pimpl_->dep_clean.end())
2155 void Buffer::markDepClean(string const & name)
2157 pimpl_->dep_clean[name] = true;
2161 bool Buffer::dispatch(string const & command, bool * result)
2163 // Split command string into command and argument
2165 string line = ltrim(command);
2166 string const arg = trim(split(line, cmd, ' '));
2168 return dispatch(lyxaction.LookupFunc(cmd), arg, result);
2172 bool Buffer::dispatch(int action, string const & argument, bool * result)
2174 bool dispatched = true;
2178 bool const tmp = Exporter::Export(this, argument, false);
2191 void Buffer::changeLanguage(Language const * from, Language const * to)
2193 lyxerr << "Changing Language!" << endl;
2195 // Take care of l10n/i18n
2198 ParIterator end = par_iterator_end();
2199 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2200 it->changeLanguage(params(), from, to);
2204 void Buffer::updateDocLang(Language const * nlang)
2206 pimpl_->messages.reset(new Messages(nlang->code()));
2210 bool Buffer::isMultiLingual()
2212 ParIterator end = par_iterator_end();
2213 for (ParIterator it = par_iterator_begin(); it != end; ++it)
2214 if (it->isMultiLingual(params()))
2221 void Buffer::inset_iterator::setParagraph()
2223 while (pit != pend) {
2224 it = pit->insetlist.begin();
2225 if (it != pit->insetlist.end())
2232 InsetOld * Buffer::getInsetFromID(int id_arg) const
2234 for (inset_iterator it = inset_const_iterator_begin();
2235 it != inset_const_iterator_end(); ++it)
2237 if (it->id() == id_arg)
2239 InsetOld * in = it->getInsetFromID(id_arg);
2247 ParIterator Buffer::getParFromID(int id) const
2249 #warning FIXME: const correctness! (Andre)
2250 ParIterator it = const_cast<Buffer*>(this)->par_iterator_begin();
2251 ParIterator end = const_cast<Buffer*>(this)->par_iterator_end();
2253 #warning FIXME, perhaps this func should return a ParIterator? (Lgb)
2255 // John says this is called with id == -1 from undo
2256 lyxerr << "getParFromID(), id: " << id << endl;
2260 for (; it != end; ++it)
2268 bool Buffer::hasParWithID(int id) const
2270 ParConstIterator it = par_iterator_begin();
2271 ParConstIterator end = par_iterator_end();
2274 // John says this is called with id == -1 from undo
2275 lyxerr << "hasParWithID(), id: " << id << endl;
2279 for (; it != end; ++it)
2287 ParIterator Buffer::par_iterator_begin()
2289 return ParIterator(paragraphs().begin(), paragraphs());
2293 ParIterator Buffer::par_iterator_end()
2295 return ParIterator(paragraphs().end(), paragraphs());
2299 ParConstIterator Buffer::par_iterator_begin() const
2301 ParagraphList const & pars = paragraphs();
2302 return ParConstIterator(const_cast<ParagraphList&>(pars).begin(), pars);
2306 ParConstIterator Buffer::par_iterator_end() const
2308 ParagraphList const & pars = paragraphs();
2309 return ParConstIterator(const_cast<ParagraphList&>(pars).end(), pars);
2313 Language const * Buffer::getLanguage() const
2315 return params().language;
2319 string const Buffer::B_(string const & l10n) const
2321 if (pimpl_->messages.get()) {
2322 return pimpl_->messages->get(l10n);
2329 bool Buffer::isClean() const
2331 return pimpl_->lyx_clean;
2335 bool Buffer::isBakClean() const
2337 return pimpl_->bak_clean;
2341 void Buffer::markClean() const
2343 if (!pimpl_->lyx_clean) {
2344 pimpl_->lyx_clean = true;
2347 // if the .lyx file has been saved, we don't need an
2349 pimpl_->bak_clean = true;
2353 void Buffer::markBakClean()
2355 pimpl_->bak_clean = true;
2359 void Buffer::setUnnamed(bool flag)
2361 pimpl_->unnamed = flag;
2365 bool Buffer::isUnnamed()
2367 return pimpl_->unnamed;
2371 void Buffer::markDirty()
2373 if (pimpl_->lyx_clean) {
2374 pimpl_->lyx_clean = false;
2377 pimpl_->bak_clean = false;
2379 DepClean::iterator it = pimpl_->dep_clean.begin();
2380 DepClean::const_iterator const end = pimpl_->dep_clean.end();
2382 for (; it != end; ++it) {
2388 string const & Buffer::fileName() const
2390 return pimpl_->filename;
2394 string const & Buffer::filePath() const
2396 return pimpl_->filepath;
2400 bool Buffer::isReadonly() const
2402 return pimpl_->read_only;
2406 void Buffer::setParentName(string const & name)
2408 params().parentname = name;
2412 Buffer::inset_iterator::inset_iterator()
2417 Buffer::inset_iterator::inset_iterator(base_type p, base_type e)
2424 Buffer::inset_iterator Buffer::inset_iterator_begin()
2426 return inset_iterator(paragraphs().begin(), paragraphs().end());
2430 Buffer::inset_iterator Buffer::inset_iterator_end()
2432 return inset_iterator(paragraphs().end(), paragraphs().end());
2436 Buffer::inset_iterator Buffer::inset_const_iterator_begin() const
2438 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2439 return inset_iterator(pars.begin(), pars.end());
2443 Buffer::inset_iterator Buffer::inset_const_iterator_end() const
2445 ParagraphList & pars = const_cast<ParagraphList&>(paragraphs());
2446 return inset_iterator(pars.end(), pars.end());
2450 Buffer::inset_iterator & Buffer::inset_iterator::operator++()
2454 if (it == pit->insetlist.end()) {
2463 Buffer::inset_iterator Buffer::inset_iterator::operator++(int)
2465 inset_iterator tmp = *this;
2471 Buffer::inset_iterator::reference Buffer::inset_iterator::operator*()
2477 Buffer::inset_iterator::pointer Buffer::inset_iterator::operator->()
2483 ParagraphList::iterator Buffer::inset_iterator::getPar() const
2489 lyx::pos_type Buffer::inset_iterator::getPos() const
2495 bool operator==(Buffer::inset_iterator const & iter1,
2496 Buffer::inset_iterator const & iter2)
2498 return iter1.pit == iter2.pit
2499 && (iter1.pit == iter1.pend || iter1.it == iter2.it);
2503 bool operator!=(Buffer::inset_iterator const & iter1,
2504 Buffer::inset_iterator const & iter2)
2506 return !(iter1 == iter2);