]> git.lyx.org Git - lyx.git/blob - src/Buffer.cpp
975993dc94c7097b2b3c70cbfb7497098f438e02
[lyx.git] / src / Buffer.cpp
1 /**
2  * \file Buffer.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Buffer.h"
14
15 #include "Author.h"
16 #include "BiblioInfo.h"
17 #include "BranchList.h"
18 #include "buffer_funcs.h"
19 #include "BufferList.h"
20 #include "BufferParams.h"
21 #include "Counters.h"
22 #include "Bullet.h"
23 #include "Chktex.h"
24 #include "debug.h"
25 #include "DocIterator.h"
26 #include "Encoding.h"
27 #include "ErrorList.h"
28 #include "Exporter.h"
29 #include "Format.h"
30 #include "FuncRequest.h"
31 #include "gettext.h"
32 #include "InsetIterator.h"
33 #include "InsetList.h"
34 #include "Language.h"
35 #include "LaTeX.h"
36 #include "LaTeXFeatures.h"
37 #include "Layout.h"
38 #include "LyXAction.h"
39 #include "Lexer.h"
40 #include "Text.h"
41 #include "LyX.h"
42 #include "LyXRC.h"
43 #include "LyXVC.h"
44 #include "Messages.h"
45 #include "output.h"
46 #include "output_docbook.h"
47 #include "output_latex.h"
48 #include "Paragraph.h"
49 #include "paragraph_funcs.h"
50 #include "ParagraphParameters.h"
51 #include "ParIterator.h"
52 #include "Session.h"
53 #include "sgml.h"
54 #include "TexRow.h"
55 #include "TextClassList.h"
56 #include "TexStream.h"
57 #include "TocBackend.h"
58 #include "Undo.h"
59 #include "version.h"
60 #include "EmbeddedFiles.h"
61 #include "PDFOptions.h"
62
63 #include "insets/InsetBibitem.h"
64 #include "insets/InsetBibtex.h"
65 #include "insets/InsetInclude.h"
66 #include "insets/InsetText.h"
67
68 #include "mathed/MathMacroTemplate.h"
69 #include "mathed/MacroTable.h"
70 #include "mathed/MathSupport.h"
71
72 #include "frontends/alert.h"
73 #include "frontends/Delegates.h"
74 #include "frontends/WorkAreaManager.h"
75 #include "frontends/FileDialog.h"
76
77 #include "graphics/Previews.h"
78
79 #include "support/types.h"
80 #include "support/lyxalgo.h"
81 #include "support/FileFilterList.h"
82 #include "support/filetools.h"
83 #include "support/Forkedcall.h"
84 #include "support/fs_extras.h"
85 #include "support/gzstream.h"
86 #include "support/lyxlib.h"
87 #include "support/os.h"
88 #include "support/Path.h"
89 #include "support/textutils.h"
90 #include "support/convert.h"
91
92 #if !defined (HAVE_FORK)
93 # define fork() -1
94 #endif
95
96 #include <boost/bind.hpp>
97 #include <boost/filesystem/exception.hpp>
98 #include <boost/filesystem/operations.hpp>
99 #include <boost/shared_ptr.hpp>
100
101 #include <algorithm>
102 #include <iomanip>
103 #include <stack>
104 #include <sstream>
105 #include <fstream>
106
107 using std::endl;
108 using std::for_each;
109 using std::make_pair;
110
111 using std::ios;
112 using std::map;
113 using std::ostream;
114 using std::ostringstream;
115 using std::ofstream;
116 using std::ifstream;
117 using std::pair;
118 using std::stack;
119 using std::vector;
120 using std::string;
121 using std::time_t;
122
123 namespace lyx {
124
125 using support::addName;
126 using support::bformat;
127 using support::changeExtension;
128 using support::cmd_ret;
129 using support::createBufferTmpDir;
130 using support::destroyDir;
131 using support::FileName;
132 using support::getFormatFromContents;
133 using support::libFileSearch;
134 using support::latex_path;
135 using support::ltrim;
136 using support::makeAbsPath;
137 using support::makeDisplayPath;
138 using support::makeLatexName;
139 using support::onlyFilename;
140 using support::onlyPath;
141 using support::quoteName;
142 using support::removeAutosaveFile;
143 using support::rename;
144 using support::runCommand;
145 using support::split;
146 using support::subst;
147 using support::tempName;
148 using support::trim;
149 using support::sum;
150 using support::suffixIs;
151
152 namespace Alert = frontend::Alert;
153 namespace os = support::os;
154 namespace fs = boost::filesystem;
155
156 namespace {
157
158 int const LYX_FORMAT = 295; //Uwe: htmlurl, href
159
160 } // namespace anon
161
162
163 typedef std::map<string, bool> DepClean;
164
165 class Buffer::Impl
166 {
167 public:
168         Impl(Buffer & parent, FileName const & file, bool readonly);
169         
170         BufferParams params;
171         LyXVC lyxvc;
172         string temppath;
173         TexRow texrow;
174
175         /// need to regenerate .tex?
176         DepClean dep_clean;
177
178         /// is save needed?
179         mutable bool lyx_clean;
180
181         /// is autosave needed?
182         mutable bool bak_clean;
183
184         /// is this a unnamed file (New...)?
185         bool unnamed;
186
187         /// buffer is r/o
188         bool read_only;
189
190         /// name of the file the buffer is associated with.
191         FileName filename;
192
193         /** Set to true only when the file is fully loaded.
194          *  Used to prevent the premature generation of previews
195          *  and by the citation inset.
196          */
197         bool file_fully_loaded;
198
199         /// our Text that should be wrapped in an InsetText
200         InsetText inset;
201
202         ///
203         MacroTable macros;
204
205         ///
206         TocBackend toc_backend;
207
208         /// Container for all sort of Buffer dependant errors.
209         map<string, ErrorList> errorLists;
210
211         /// all embedded files of this buffer
212         EmbeddedFiles embedded_files;
213
214         /// timestamp and checksum used to test if the file has been externally
215         /// modified. (Used to properly enable 'File->Revert to saved', bug 4114).
216         time_t timestamp_;
217         unsigned long checksum_;
218
219         ///
220         frontend::WorkAreaManager * wa_;
221
222         ///
223         Undo undo_;
224 };
225
226
227 Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
228         : lyx_clean(true), bak_clean(true), unnamed(false), read_only(readonly_),
229           filename(file), file_fully_loaded(false), inset(params),
230           toc_backend(&parent), embedded_files(&parent), timestamp_(0),
231           checksum_(0), wa_(0), undo_(parent)
232 {
233         inset.setAutoBreakRows(true);
234         lyxvc.buffer(&parent);
235         temppath = createBufferTmpDir();
236         params.filepath = onlyPath(file.absFilename());
237         // FIXME: And now do something if temppath == string(), because we
238         // assume from now on that temppath points to a valid temp dir.
239         // See http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg67406.html
240
241         if (use_gui)
242                 wa_ = new frontend::WorkAreaManager;
243 }
244
245
246 Buffer::Buffer(string const & file, bool readonly)
247         : pimpl_(new Impl(*this, FileName(file), readonly)), gui_(0)
248 {
249         LYXERR(Debug::INFO) << "Buffer::Buffer()" << endl;
250 }
251
252
253 Buffer::~Buffer()
254 {
255         LYXERR(Debug::INFO) << "Buffer::~Buffer()" << endl;
256         // here the buffer should take care that it is
257         // saved properly, before it goes into the void.
258
259         Buffer * master = masterBuffer();
260         if (master != this && use_gui)
261                 // We are closing buf which was a child document so we
262                 // must update the labels and section numbering of its master
263                 // Buffer.
264                 updateLabels(*master);
265
266         if (!temppath().empty() && !destroyDir(FileName(temppath()))) {
267                 Alert::warning(_("Could not remove temporary directory"),
268                         bformat(_("Could not remove the temporary directory %1$s"),
269                         from_utf8(temppath())));
270         }
271
272         // Remove any previewed LaTeX snippets associated with this buffer.
273         graphics::Previews::get().removeLoader(*this);
274
275         if (pimpl_->wa_) {
276                 pimpl_->wa_->closeAll();
277                 delete pimpl_->wa_;
278         }
279         delete pimpl_;
280 }
281
282
283 void Buffer::changed() const
284 {
285         if (pimpl_->wa_)
286                 pimpl_->wa_->redrawAll();
287 }
288
289
290 frontend::WorkAreaManager & Buffer::workAreaManager() const
291 {
292         BOOST_ASSERT(pimpl_->wa_);
293         return *pimpl_->wa_;
294 }
295
296
297 Text & Buffer::text() const
298 {
299         return const_cast<Text &>(pimpl_->inset.text_);
300 }
301
302
303 Inset & Buffer::inset() const
304 {
305         return const_cast<InsetText &>(pimpl_->inset);
306 }
307
308
309 BufferParams & Buffer::params()
310 {
311         return pimpl_->params;
312 }
313
314
315 BufferParams const & Buffer::params() const
316 {
317         return pimpl_->params;
318 }
319
320
321 ParagraphList & Buffer::paragraphs()
322 {
323         return text().paragraphs();
324 }
325
326
327 ParagraphList const & Buffer::paragraphs() const
328 {
329         return text().paragraphs();
330 }
331
332
333 LyXVC & Buffer::lyxvc()
334 {
335         return pimpl_->lyxvc;
336 }
337
338
339 LyXVC const & Buffer::lyxvc() const
340 {
341         return pimpl_->lyxvc;
342 }
343
344
345 string const & Buffer::temppath() const
346 {
347         return pimpl_->temppath;
348 }
349
350
351 TexRow & Buffer::texrow()
352 {
353         return pimpl_->texrow;
354 }
355
356
357 TexRow const & Buffer::texrow() const
358 {
359         return pimpl_->texrow;
360 }
361
362
363 TocBackend & Buffer::tocBackend()
364 {
365         return pimpl_->toc_backend;
366 }
367
368
369 TocBackend const & Buffer::tocBackend() const
370 {
371         return pimpl_->toc_backend;
372 }
373
374
375 EmbeddedFiles & Buffer::embeddedFiles()
376 {
377         return pimpl_->embedded_files;
378 }
379
380
381 EmbeddedFiles const & Buffer::embeddedFiles() const
382 {
383         return pimpl_->embedded_files;
384 }
385
386
387 Undo & Buffer::undo()
388 {
389         return pimpl_->undo_;
390 }
391
392
393 string Buffer::latexName(bool const no_path) const
394 {
395         string const name = changeExtension(makeLatexName(absFileName()), ".tex");
396         return no_path ? onlyFilename(name) : name;
397 }
398
399
400 pair<Buffer::LogType, string> Buffer::logName() const
401 {
402         string const filename = latexName(false);
403
404         if (filename.empty())
405                 return make_pair(Buffer::latexlog, string());
406
407         string const path = temppath();
408
409         FileName const fname(addName(temppath(),
410                                      onlyFilename(changeExtension(filename,
411                                                                   ".log"))));
412         FileName const bname(
413                 addName(path, onlyFilename(
414                         changeExtension(filename,
415                                         formats.extension("literate") + ".out"))));
416
417         // If no Latex log or Build log is newer, show Build log
418
419         if (bname.exists() &&
420             (!fname.exists() || fname.lastModified() < bname.lastModified())) {
421                 LYXERR(Debug::FILES) << "Log name calculated as: " << bname << endl;
422                 return make_pair(Buffer::buildlog, bname.absFilename());
423         }
424         LYXERR(Debug::FILES) << "Log name calculated as: " << fname << endl;
425         return make_pair(Buffer::latexlog, fname.absFilename());
426 }
427
428
429 void Buffer::setReadonly(bool const flag)
430 {
431         if (pimpl_->read_only != flag) {
432                 pimpl_->read_only = flag;
433                 readonly(flag);
434         }
435 }
436
437
438 void Buffer::setFileName(string const & newfile)
439 {
440         pimpl_->filename = makeAbsPath(newfile);
441         params().filepath = onlyPath(pimpl_->filename.absFilename());
442         setReadonly(pimpl_->filename.isReadOnly());
443         updateTitles();
444 }
445
446
447 int Buffer::readHeader(Lexer & lex)
448 {
449         int unknown_tokens = 0;
450         int line = -1;
451         int begin_header_line = -1;
452
453         // Initialize parameters that may be/go lacking in header:
454         params().branchlist().clear();
455         params().preamble.erase();
456         params().options.erase();
457         params().float_placement.erase();
458         params().paperwidth.erase();
459         params().paperheight.erase();
460         params().leftmargin.erase();
461         params().rightmargin.erase();
462         params().topmargin.erase();
463         params().bottommargin.erase();
464         params().headheight.erase();
465         params().headsep.erase();
466         params().footskip.erase();
467         params().listings_params.clear();
468         params().clearLayoutModules();
469         params().pdfoptions().clear();
470         
471         for (int i = 0; i < 4; ++i) {
472                 params().user_defined_bullet(i) = ITEMIZE_DEFAULTS[i];
473                 params().temp_bullet(i) = ITEMIZE_DEFAULTS[i];
474         }
475
476         ErrorList & errorList = pimpl_->errorLists["Parse"];
477
478         while (lex.isOK()) {
479                 lex.next();
480                 string const token = lex.getString();
481
482                 if (token.empty())
483                         continue;
484
485                 if (token == "\\end_header")
486                         break;
487
488                 ++line;
489                 if (token == "\\begin_header") {
490                         begin_header_line = line;
491                         continue;
492                 }
493
494                 LYXERR(Debug::PARSER) << "Handling document header token: `"
495                                       << token << '\'' << endl;
496
497                 string unknown = params().readToken(lex, token);
498                 if (!unknown.empty()) {
499                         if (unknown[0] != '\\' && token == "\\textclass") {
500                                 Alert::warning(_("Unknown document class"),
501                        bformat(_("Using the default document class, because the "
502                                               "class %1$s is unknown."), from_utf8(unknown)));
503                         } else {
504                                 ++unknown_tokens;
505                                 docstring const s = bformat(_("Unknown token: "
506                                                                         "%1$s %2$s\n"),
507                                                          from_utf8(token),
508                                                          lex.getDocString());
509                                 errorList.push_back(ErrorItem(_("Document header error"),
510                                         s, -1, 0, 0));
511                         }
512                 }
513         }
514         if (begin_header_line) {
515                 docstring const s = _("\\begin_header is missing");
516                 errorList.push_back(ErrorItem(_("Document header error"),
517                         s, -1, 0, 0));
518         }
519
520         return unknown_tokens;
521 }
522
523
524 // Uwe C. Schroeder
525 // changed to be public and have one parameter
526 // Returns false if "\end_document" is not read (Asger)
527 bool Buffer::readDocument(Lexer & lex)
528 {
529         ErrorList & errorList = pimpl_->errorLists["Parse"];
530         errorList.clear();
531
532         lex.next();
533         string const token = lex.getString();
534         if (token != "\\begin_document") {
535                 docstring const s = _("\\begin_document is missing");
536                 errorList.push_back(ErrorItem(_("Document header error"),
537                         s, -1, 0, 0));
538         }
539
540         // we are reading in a brand new document
541         BOOST_ASSERT(paragraphs().empty());
542
543         readHeader(lex);
544         TextClass const & baseClass = textclasslist[params().getBaseClass()];
545         if (!baseClass.load(filePath())) {
546                 string theclass = baseClass.name();
547                 Alert::error(_("Can't load document class"), bformat(
548                         _("Using the default document class, because the "
549                                      "class %1$s could not be loaded."), from_utf8(theclass)));
550                 params().setBaseClass(defaultTextclass());
551         }
552
553         if (params().outputChanges) {
554                 bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
555                 bool xcolorsoul = LaTeXFeatures::isAvailable("soul") &&
556                                   LaTeXFeatures::isAvailable("xcolor");
557
558                 if (!dvipost && !xcolorsoul) {
559                         Alert::warning(_("Changes not shown in LaTeX output"),
560                                        _("Changes will not be highlighted in LaTeX output, "
561                                          "because neither dvipost nor xcolor/soul are installed.\n"
562                                          "Please install these packages or redefine "
563                                          "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
564                 } else if (!xcolorsoul) {
565                         Alert::warning(_("Changes not shown in LaTeX output"),
566                                        _("Changes will not be highlighted in LaTeX output "
567                                          "when using pdflatex, because xcolor and soul are not installed.\n"
568                                          "Please install both packages or redefine "
569                                          "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
570                 }
571         }
572         // read manifest after header
573         embeddedFiles().readManifest(lex, errorList);   
574
575         // read main text
576         bool const res = text().read(*this, lex, errorList);
577         for_each(text().paragraphs().begin(),
578                  text().paragraphs().end(),
579                  bind(&Paragraph::setInsetOwner, _1, &inset()));
580
581         return res;
582 }
583
584
585 // needed to insert the selection
586 void Buffer::insertStringAsLines(ParagraphList & pars,
587         pit_type & pit, pos_type & pos,
588         Font const & fn, docstring const & str, bool autobreakrows)
589 {
590         Font font = fn;
591
592         // insert the string, don't insert doublespace
593         bool space_inserted = true;
594         for (docstring::const_iterator cit = str.begin();
595             cit != str.end(); ++cit) {
596                 Paragraph & par = pars[pit];
597                 if (*cit == '\n') {
598                         if (autobreakrows && (!par.empty() || par.allowEmpty())) {
599                                 breakParagraph(params(), pars, pit, pos,
600                                                par.layout()->isEnvironment());
601                                 ++pit;
602                                 pos = 0;
603                                 space_inserted = true;
604                         } else {
605                                 continue;
606                         }
607                         // do not insert consecutive spaces if !free_spacing
608                 } else if ((*cit == ' ' || *cit == '\t') &&
609                            space_inserted && !par.isFreeSpacing()) {
610                         continue;
611                 } else if (*cit == '\t') {
612                         if (!par.isFreeSpacing()) {
613                                 // tabs are like spaces here
614                                 par.insertChar(pos, ' ', font, params().trackChanges);
615                                 ++pos;
616                                 space_inserted = true;
617                         } else {
618                                 const pos_type n = 8 - pos % 8;
619                                 for (pos_type i = 0; i < n; ++i) {
620                                         par.insertChar(pos, ' ', font, params().trackChanges);
621                                         ++pos;
622                                 }
623                                 space_inserted = true;
624                         }
625                 } else if (!isPrintable(*cit)) {
626                         // Ignore unprintables
627                         continue;
628                 } else {
629                         // just insert the character
630                         par.insertChar(pos, *cit, font, params().trackChanges);
631                         ++pos;
632                         space_inserted = (*cit == ' ');
633                 }
634
635         }
636 }
637
638
639 bool Buffer::readString(std::string const & s)
640 {
641         params().compressed = false;
642
643         // remove dummy empty par
644         paragraphs().clear();
645         Lexer lex(0, 0);
646         std::istringstream is(s);
647         lex.setStream(is);
648         FileName const name(tempName());
649         switch (readFile(lex, name, true)) {
650         case failure:
651                 return false;
652         case wrongversion: {
653                 // We need to call lyx2lyx, so write the input to a file
654                 std::ofstream os(name.toFilesystemEncoding().c_str());
655                 os << s;
656                 os.close();
657                 return readFile(name);
658         }
659         case success:
660                 break;
661         }
662
663         return true;
664 }
665
666
667 bool Buffer::readFile(FileName const & filename)
668 {
669         FileName fname(filename);
670         // Check if the file is compressed.
671         string format = getFormatFromContents(filename);
672         if (format == "zip") {
673                 // decompress to a temp directory
674                 LYXERR(Debug::FILES) << filename << " is in zip format. Unzip to " << temppath() << endl;
675                 ::unzipToDir(filename.toFilesystemEncoding(), temppath());
676                 //
677                 FileName lyxfile(addName(temppath(), "content.lyx"));
678                 // if both manifest.txt and file.lyx exist, this is am embedded file
679                 if (lyxfile.exists()) {
680                         params().embedded = true;
681                         fname = lyxfile;
682                 }
683         }
684         // The embedded lyx file can also be compressed, for backward compatibility
685         format = getFormatFromContents(fname);
686         if (format == "gzip" || format == "zip" || format == "compress") {
687                 params().compressed = true;
688         }
689
690         // remove dummy empty par
691         paragraphs().clear();
692         Lexer lex(0, 0);
693         lex.setFile(fname);
694         if (readFile(lex, fname) != success)
695                 return false;
696
697         return true;
698 }
699
700
701 bool Buffer::isFullyLoaded() const
702 {
703         return pimpl_->file_fully_loaded;
704 }
705
706
707 void Buffer::setFullyLoaded(bool value)
708 {
709         pimpl_->file_fully_loaded = value;
710 }
711
712
713 Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
714                 bool fromstring)
715 {
716         BOOST_ASSERT(!filename.empty());
717
718         if (!lex.isOK()) {
719                 Alert::error(_("Document could not be read"),
720                              bformat(_("%1$s could not be read."), from_utf8(filename.absFilename())));
721                 return failure;
722         }
723
724         lex.next();
725         string const token = lex.getString();
726
727         if (!lex) {
728                 Alert::error(_("Document could not be read"),
729                              bformat(_("%1$s could not be read."), from_utf8(filename.absFilename())));
730                 return failure;
731         }
732
733         // the first token _must_ be...
734         if (token != "\\lyxformat") {
735                 lyxerr << "Token: " << token << endl;
736
737                 Alert::error(_("Document format failure"),
738                              bformat(_("%1$s is not a LyX document."),
739                                        from_utf8(filename.absFilename())));
740                 return failure;
741         }
742
743         lex.next();
744         string tmp_format = lex.getString();
745         //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
746         // if present remove ".," from string.
747         string::size_type dot = tmp_format.find_first_of(".,");
748         //lyxerr << "           dot found at " << dot << endl;
749         if (dot != string::npos)
750                         tmp_format.erase(dot, 1);
751         int const file_format = convert<int>(tmp_format);
752         //lyxerr << "format: " << file_format << endl;
753
754         // save timestamp and checksum of the original disk file, making sure
755         // to not overwrite them with those of the file created in the tempdir
756         // when it has to be converted to the current format.
757         if (!pimpl_->checksum_) {
758                 // Save the timestamp and checksum of disk file. If filename is an
759                 // emergency file, save the timestamp and sum of the original lyx file
760                 // because isExternallyModified will check for this file. (BUG4193)
761                 string diskfile = filename.toFilesystemEncoding();
762                 if (suffixIs(diskfile, ".emergency"))
763                         diskfile = diskfile.substr(0, diskfile.size() - 10);
764                 saveCheckSum(FileName(diskfile));
765         }
766
767         if (file_format != LYX_FORMAT) {
768
769                 if (fromstring)
770                         // lyx2lyx would fail
771                         return wrongversion;
772
773                 FileName const tmpfile(tempName());
774                 if (tmpfile.empty()) {
775                         Alert::error(_("Conversion failed"),
776                                      bformat(_("%1$s is from a different"
777                                               " version of LyX, but a temporary"
778                                               " file for converting it could"
779                                                             " not be created."),
780                                               from_utf8(filename.absFilename())));
781                         return failure;
782                 }
783                 FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
784                 if (lyx2lyx.empty()) {
785                         Alert::error(_("Conversion script not found"),
786                                      bformat(_("%1$s is from a different"
787                                                " version of LyX, but the"
788                                                " conversion script lyx2lyx"
789                                                             " could not be found."),
790                                                from_utf8(filename.absFilename())));
791                         return failure;
792                 }
793                 ostringstream command;
794                 command << os::python()
795                         << ' ' << quoteName(lyx2lyx.toFilesystemEncoding())
796                         << " -t " << convert<string>(LYX_FORMAT)
797                         << " -o " << quoteName(tmpfile.toFilesystemEncoding())
798                         << ' ' << quoteName(filename.toFilesystemEncoding());
799                 string const command_str = command.str();
800
801                 LYXERR(Debug::INFO) << "Running '"
802                                     << command_str << '\''
803                                     << endl;
804
805                 cmd_ret const ret = runCommand(command_str);
806                 if (ret.first != 0) {
807                         Alert::error(_("Conversion script failed"),
808                                      bformat(_("%1$s is from a different version"
809                                               " of LyX, but the lyx2lyx script"
810                                                             " failed to convert it."),
811                                               from_utf8(filename.absFilename())));
812                         return failure;
813                 } else {
814                         bool const ret = readFile(tmpfile);
815                         // Do stuff with tmpfile name and buffer name here.
816                         return ret ? success : failure;
817                 }
818
819         }
820
821         if (readDocument(lex)) {
822                 Alert::error(_("Document format failure"),
823                              bformat(_("%1$s ended unexpectedly, which means"
824                                                     " that it is probably corrupted."),
825                                        from_utf8(filename.absFilename())));
826         }
827
828         //lyxerr << "removing " << MacroTable::localMacros().size()
829         //      << " temporary macro entries" << endl;
830         //MacroTable::localMacros().clear();
831
832         pimpl_->file_fully_loaded = true;
833         return success;
834 }
835
836
837 // Should probably be moved to somewhere else: BufferView? LyXView?
838 bool Buffer::save() const
839 {
840         // We don't need autosaves in the immediate future. (Asger)
841         resetAutosaveTimers();
842
843         string const encodedFilename = pimpl_->filename.toFilesystemEncoding();
844
845         FileName backupName;
846         bool madeBackup = false;
847
848         // make a backup if the file already exists
849         if (lyxrc.make_backup && fs::exists(encodedFilename)) {
850                 backupName = FileName(absFileName() + '~');
851                 if (!lyxrc.backupdir_path.empty())
852                         backupName = FileName(addName(lyxrc.backupdir_path,
853                                               subst(os::internal_path(backupName.absFilename()), '/', '!')));
854
855                 try {
856                         fs::copy_file(encodedFilename, backupName.toFilesystemEncoding(), false);
857                         madeBackup = true;
858                 } catch (fs::filesystem_error const & fe) {
859                         Alert::error(_("Backup failure"),
860                                      bformat(_("Cannot create backup file %1$s.\n"
861                                                "Please check whether the directory exists and is writeable."),
862                                              from_utf8(backupName.absFilename())));
863                         LYXERR(Debug::DEBUG) << "Fs error: " << fe.what() << endl;
864                 }
865         }
866
867         // ask if the disk file has been externally modified (use checksum method)
868         if (fs::exists(encodedFilename) && isExternallyModified(checksum_method)) {
869                 docstring const file = makeDisplayPath(absFileName(), 20);
870                 docstring text = bformat(_("Document %1$s has been externally modified. Are you sure "
871                                                              "you want to overwrite this file?"), file);
872                 int const ret = Alert::prompt(_("Overwrite modified file?"),
873                         text, 1, 1, _("&Overwrite"), _("&Cancel"));
874                 if (ret == 1)
875                         return false;
876         }
877
878         if (writeFile(pimpl_->filename)) {
879                 markClean();
880                 removeAutosaveFile(absFileName());
881                 saveCheckSum(pimpl_->filename);
882                 return true;
883         } else {
884                 // Saving failed, so backup is not backup
885                 if (madeBackup)
886                         rename(backupName, pimpl_->filename);
887                 return false;
888         }
889 }
890
891
892 bool Buffer::writeFile(FileName const & fname) const
893 {
894         if (pimpl_->read_only && fname == pimpl_->filename)
895                 return false;
896
897         bool retval = false;
898
899         FileName content;
900         if (params().embedded)
901                 // first write the .lyx file to the temporary directory
902                 content = FileName(addName(temppath(), "content.lyx"));
903         else
904                 content = fname;
905         
906         if (params().compressed) {
907                 gz::ogzstream ofs(content.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
908                 if (!ofs)
909                         return false;
910
911                 retval = write(ofs);
912         } else {
913                 ofstream ofs(content.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
914                 if (!ofs)
915                         return false;
916
917                 retval = write(ofs);
918         }
919
920         if (retval && params().embedded) {
921                 // write file.lyx and all the embedded files to the zip file fname
922                 // if embedding is enabled
923                 return pimpl_->embedded_files.writeFile(fname);
924         }
925         return retval;
926 }
927
928
929 bool Buffer::write(ostream & ofs) const
930 {
931 #ifdef HAVE_LOCALE
932         // Use the standard "C" locale for file output.
933         ofs.imbue(std::locale::classic());
934 #endif
935
936         // The top of the file should not be written by params().
937
938         // write out a comment in the top of the file
939         ofs << "#LyX " << lyx_version
940             << " created this file. For more info see http://www.lyx.org/\n"
941             << "\\lyxformat " << LYX_FORMAT << "\n"
942             << "\\begin_document\n";
943
944
945         /// For each author, set 'used' to true if there is a change
946         /// by this author in the document; otherwise set it to 'false'.
947         AuthorList::Authors::const_iterator a_it = params().authors().begin();
948         AuthorList::Authors::const_iterator a_end = params().authors().end();
949         for (; a_it != a_end; ++a_it)
950                 a_it->second.used(false);
951
952         ParIterator const end = par_iterator_end();
953         ParIterator it = par_iterator_begin();
954         for ( ; it != end; ++it)
955                 it->checkAuthors(params().authors());
956
957         // now write out the buffer parameters.
958         ofs << "\\begin_header\n";
959         params().writeFile(ofs);
960         ofs << "\\end_header\n";
961
962         // write the manifest after header
963         ofs << "\n\\begin_manifest\n";
964         pimpl_->embedded_files.update();
965         embeddedFiles().writeManifest(ofs);
966         ofs << "\\end_manifest\n";
967
968         // write the text
969         ofs << "\n\\begin_body\n";
970         text().write(*this, ofs);
971         ofs << "\n\\end_body\n";
972
973         // Write marker that shows file is complete
974         ofs << "\\end_document" << endl;
975
976         // Shouldn't really be needed....
977         //ofs.close();
978
979         // how to check if close went ok?
980         // Following is an attempt... (BE 20001011)
981
982         // good() returns false if any error occured, including some
983         //        formatting error.
984         // bad()  returns true if something bad happened in the buffer,
985         //        which should include file system full errors.
986
987         bool status = true;
988         if (!ofs) {
989                 status = false;
990                 lyxerr << "File was not closed properly." << endl;
991         }
992
993         return status;
994 }
995
996
997 bool Buffer::makeLaTeXFile(FileName const & fname,
998                            string const & original_path,
999                            OutputParams const & runparams,
1000                            bool output_preamble, bool output_body)
1001 {
1002         string const encoding = runparams.encoding->iconvName();
1003         LYXERR(Debug::LATEX) << "makeLaTeXFile encoding: "
1004                 << encoding << "..." << endl;
1005
1006         odocfstream ofs(encoding);
1007         if (!openFileWrite(ofs, fname))
1008                 return false;
1009
1010         //TexStream ts(ofs.rdbuf(), &texrow());
1011
1012         bool failed_export = false;
1013         try {
1014                 texrow().reset();
1015                 writeLaTeXSource(ofs, original_path,
1016                       runparams, output_preamble, output_body);
1017         }
1018         catch (iconv_codecvt_facet_exception & e) {
1019                 lyxerr << "Caught iconv exception: " << e.what() << endl;
1020                 failed_export = true;
1021         }
1022         catch (std::exception const & e) {
1023                 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
1024                 failed_export = true;
1025         }
1026         catch (...) {
1027                 lyxerr << "Caught some really weird exception..." << endl;
1028                 LyX::cref().emergencyCleanup();
1029                 abort();
1030         }
1031
1032         ofs.close();
1033         if (ofs.fail()) {
1034                 failed_export = true;
1035                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1036         }
1037
1038         if (failed_export) {
1039                 Alert::error(_("Encoding error"),
1040                         _("Some characters of your document are probably not "
1041                         "representable in the chosen encoding.\n"
1042                         "Changing the document encoding to utf8 could help."));
1043                 return false;
1044         }
1045         return true;
1046 }
1047
1048
1049 void Buffer::writeLaTeXSource(odocstream & os,
1050                            string const & original_path,
1051                            OutputParams const & runparams_in,
1052                            bool const output_preamble, bool const output_body)
1053 {
1054         OutputParams runparams = runparams_in;
1055
1056         // validate the buffer.
1057         LYXERR(Debug::LATEX) << "  Validating buffer..." << endl;
1058         LaTeXFeatures features(*this, params(), runparams);
1059         validate(features);
1060         LYXERR(Debug::LATEX) << "  Buffer validation done." << endl;
1061
1062         // The starting paragraph of the coming rows is the
1063         // first paragraph of the document. (Asger)
1064         if (output_preamble && runparams.nice) {
1065                 os << "%% LyX " << lyx_version << " created this file.  "
1066                         "For more info, see http://www.lyx.org/.\n"
1067                         "%% Do not edit unless you really know what "
1068                         "you are doing.\n";
1069                 texrow().newline();
1070                 texrow().newline();
1071         }
1072         LYXERR(Debug::INFO) << "lyx document header finished" << endl;
1073         // There are a few differences between nice LaTeX and usual files:
1074         // usual is \batchmode and has a
1075         // special input@path to allow the including of figures
1076         // with either \input or \includegraphics (what figinsets do).
1077         // input@path is set when the actual parameter
1078         // original_path is set. This is done for usual tex-file, but not
1079         // for nice-latex-file. (Matthias 250696)
1080         // Note that input@path is only needed for something the user does
1081         // in the preamble, included .tex files or ERT, files included by
1082         // LyX work without it.
1083         if (output_preamble) {
1084                 if (!runparams.nice) {
1085                         // code for usual, NOT nice-latex-file
1086                         os << "\\batchmode\n"; // changed
1087                         // from \nonstopmode
1088                         texrow().newline();
1089                 }
1090                 if (!original_path.empty()) {
1091                         // FIXME UNICODE
1092                         // We don't know the encoding of inputpath
1093                         docstring const inputpath = from_utf8(latex_path(original_path));
1094                         os << "\\makeatletter\n"
1095                            << "\\def\\input@path{{"
1096                            << inputpath << "/}}\n"
1097                            << "\\makeatother\n";
1098                         texrow().newline();
1099                         texrow().newline();
1100                         texrow().newline();
1101                 }
1102
1103                 // Write the preamble
1104                 runparams.use_babel = params().writeLaTeX(os, features, texrow());
1105
1106                 if (!output_body)
1107                         return;
1108
1109                 // make the body.
1110                 os << "\\begin{document}\n";
1111                 texrow().newline();
1112         } // output_preamble
1113
1114         texrow().start(paragraphs().begin()->id(), 0);
1115         
1116         LYXERR(Debug::INFO) << "preamble finished, now the body." << endl;
1117
1118         if (!lyxrc.language_auto_begin &&
1119             !params().language->babel().empty()) {
1120                 // FIXME UNICODE
1121                 os << from_utf8(subst(lyxrc.language_command_begin,
1122                                            "$$lang",
1123                                            params().language->babel()))
1124                    << '\n';
1125                 texrow().newline();
1126         }
1127
1128         Encoding const & encoding = params().encoding();
1129         if (encoding.package() == Encoding::CJK) {
1130                 // Open a CJK environment, since in contrast to the encodings
1131                 // handled by inputenc the document encoding is not set in
1132                 // the preamble if it is handled by CJK.sty.
1133                 os << "\\begin{CJK}{" << from_ascii(encoding.latexName())
1134                    << "}{}\n";
1135                 texrow().newline();
1136         }
1137
1138         // if we are doing a real file with body, even if this is the
1139         // child of some other buffer, let's cut the link here.
1140         // This happens for example if only a child document is printed.
1141         string save_parentname;
1142         if (output_preamble) {
1143                 save_parentname = params().parentname;
1144                 params().parentname.erase();
1145         }
1146
1147         loadChildDocuments();
1148
1149         // the real stuff
1150         latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1151
1152         // Restore the parenthood if needed
1153         if (output_preamble)
1154                 params().parentname = save_parentname;
1155
1156         // add this just in case after all the paragraphs
1157         os << endl;
1158         texrow().newline();
1159
1160         if (encoding.package() == Encoding::CJK) {
1161                 // Close the open CJK environment.
1162                 // latexParagraphs will have opened one even if the last text
1163                 // was not CJK.
1164                 os << "\\end{CJK}\n";
1165                 texrow().newline();
1166         }
1167
1168         if (!lyxrc.language_auto_end &&
1169             !params().language->babel().empty()) {
1170                 os << from_utf8(subst(lyxrc.language_command_end,
1171                                            "$$lang",
1172                                            params().language->babel()))
1173                    << '\n';
1174                 texrow().newline();
1175         }
1176
1177         if (output_preamble) {
1178                 os << "\\end{document}\n";
1179                 texrow().newline();
1180
1181                 LYXERR(Debug::LATEX) << "makeLaTeXFile...done" << endl;
1182         } else {
1183                 LYXERR(Debug::LATEX) << "LaTeXFile for inclusion made."
1184                                      << endl;
1185         }
1186         runparams_in.encoding = runparams.encoding;
1187
1188         // Just to be sure. (Asger)
1189         texrow().newline();
1190
1191         LYXERR(Debug::INFO) << "Finished making LaTeX file." << endl;
1192         LYXERR(Debug::INFO) << "Row count was " << texrow().rows() - 1
1193                             << '.' << endl;
1194 }
1195
1196
1197 bool Buffer::isLatex() const
1198 {
1199         return params().getTextClass().outputType() == LATEX;
1200 }
1201
1202
1203 bool Buffer::isLiterate() const
1204 {
1205         return params().getTextClass().outputType() == LITERATE;
1206 }
1207
1208
1209 bool Buffer::isDocBook() const
1210 {
1211         return params().getTextClass().outputType() == DOCBOOK;
1212 }
1213
1214
1215 void Buffer::makeDocBookFile(FileName const & fname,
1216                               OutputParams const & runparams,
1217                               bool const body_only)
1218 {
1219         LYXERR(Debug::LATEX) << "makeDocBookFile..." << endl;
1220
1221         //ofstream ofs;
1222         odocfstream ofs;
1223         if (!openFileWrite(ofs, fname))
1224                 return;
1225
1226         writeDocBookSource(ofs, fname.absFilename(), runparams, body_only);
1227
1228         ofs.close();
1229         if (ofs.fail())
1230                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1231 }
1232
1233
1234 void Buffer::writeDocBookSource(odocstream & os, string const & fname,
1235                              OutputParams const & runparams,
1236                              bool const only_body)
1237 {
1238         LaTeXFeatures features(*this, params(), runparams);
1239         validate(features);
1240
1241         texrow().reset();
1242
1243         TextClass const & tclass = params().getTextClass();
1244         string const top_element = tclass.latexname();
1245
1246         if (!only_body) {
1247                 if (runparams.flavor == OutputParams::XML)
1248                         os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1249
1250                 // FIXME UNICODE
1251                 os << "<!DOCTYPE " << from_ascii(top_element) << ' ';
1252
1253                 // FIXME UNICODE
1254                 if (! tclass.class_header().empty())
1255                         os << from_ascii(tclass.class_header());
1256                 else if (runparams.flavor == OutputParams::XML)
1257                         os << "PUBLIC \"-//OASIS//DTD DocBook XML//EN\" "
1258                             << "\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\"";
1259                 else
1260                         os << " PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\"";
1261
1262                 docstring preamble = from_utf8(params().preamble);
1263                 if (runparams.flavor != OutputParams::XML ) {
1264                         preamble += "<!ENTITY % output.print.png \"IGNORE\">\n";
1265                         preamble += "<!ENTITY % output.print.pdf \"IGNORE\">\n";
1266                         preamble += "<!ENTITY % output.print.eps \"IGNORE\">\n";
1267                         preamble += "<!ENTITY % output.print.bmp \"IGNORE\">\n";
1268                 }
1269
1270                 string const name = runparams.nice
1271                         ? changeExtension(absFileName(), ".sgml") : fname;
1272                 preamble += features.getIncludedFiles(name);
1273                 preamble += features.getLyXSGMLEntities();
1274
1275                 if (!preamble.empty()) {
1276                         os << "\n [ " << preamble << " ]";
1277                 }
1278                 os << ">\n\n";
1279         }
1280
1281         string top = top_element;
1282         top += " lang=\"";
1283         if (runparams.flavor == OutputParams::XML)
1284                 top += params().language->code();
1285         else
1286                 top += params().language->code().substr(0,2);
1287         top += '"';
1288
1289         if (!params().options.empty()) {
1290                 top += ' ';
1291                 top += params().options;
1292         }
1293
1294         os << "<!-- " << ((runparams.flavor == OutputParams::XML)? "XML" : "SGML")
1295             << " file was created by LyX " << lyx_version
1296             << "\n  See http://www.lyx.org/ for more information -->\n";
1297
1298         params().getTextClass().counters().reset();
1299
1300         loadChildDocuments();
1301
1302         sgml::openTag(os, top);
1303         os << '\n';
1304         docbookParagraphs(paragraphs(), *this, os, runparams);
1305         sgml::closeTag(os, top_element);
1306 }
1307
1308
1309 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1310 // Other flags: -wall -v0 -x
1311 int Buffer::runChktex()
1312 {
1313         busy(true);
1314
1315         // get LaTeX-Filename
1316         FileName const path(temppath());
1317         string const name = addName(path.absFilename(), latexName());
1318         string const org_path = filePath();
1319
1320         support::Path p(path); // path to LaTeX file
1321         message(_("Running chktex..."));
1322
1323         // Generate the LaTeX file if neccessary
1324         OutputParams runparams(&params().encoding());
1325         runparams.flavor = OutputParams::LATEX;
1326         runparams.nice = false;
1327         makeLaTeXFile(FileName(name), org_path, runparams);
1328
1329         TeXErrors terr;
1330         Chktex chktex(lyxrc.chktex_command, onlyFilename(name), filePath());
1331         int const res = chktex.run(terr); // run chktex
1332
1333         if (res == -1) {
1334                 Alert::error(_("chktex failure"),
1335                              _("Could not run chktex successfully."));
1336         } else if (res > 0) {
1337                 ErrorList & errorList = pimpl_->errorLists["ChkTeX"];
1338                 // Clear out old errors
1339                 errorList.clear();
1340                 // Fill-in the error list with the TeX errors
1341                 bufferErrors(*this, terr, errorList);
1342         }
1343
1344         busy(false);
1345
1346         errors("ChkTeX");
1347
1348         return res;
1349 }
1350
1351
1352 void Buffer::validate(LaTeXFeatures & features) const
1353 {
1354         TextClass const & tclass = params().getTextClass();
1355
1356         if (params().outputChanges) {
1357                 bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
1358                 bool xcolorsoul = LaTeXFeatures::isAvailable("soul") &&
1359                                   LaTeXFeatures::isAvailable("xcolor");
1360
1361                 if (features.runparams().flavor == OutputParams::LATEX) {
1362                         if (dvipost) {
1363                                 features.require("ct-dvipost");
1364                                 features.require("dvipost");
1365                         } else if (xcolorsoul) {
1366                                 features.require("ct-xcolor-soul");
1367                                 features.require("soul");
1368                                 features.require("xcolor");
1369                         } else {
1370                                 features.require("ct-none");
1371                         }
1372                 } else if (features.runparams().flavor == OutputParams::PDFLATEX ) {
1373                         if (xcolorsoul) {
1374                                 features.require("ct-xcolor-soul");
1375                                 features.require("soul");
1376                                 features.require("xcolor");
1377                                 features.require("pdfcolmk"); // improves color handling in PDF output
1378                         } else {
1379                                 features.require("ct-none");
1380                         }
1381                 }
1382         }
1383
1384         // AMS Style is at document level
1385         if (params().use_amsmath == BufferParams::package_on
1386             || tclass.provides("amsmath"))
1387                 features.require("amsmath");
1388         if (params().use_esint == BufferParams::package_on)
1389                 features.require("esint");
1390
1391         loadChildDocuments();
1392
1393         for_each(paragraphs().begin(), paragraphs().end(),
1394                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1395
1396         // the bullet shapes are buffer level not paragraph level
1397         // so they are tested here
1398         for (int i = 0; i < 4; ++i) {
1399                 if (params().user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1400                         int const font = params().user_defined_bullet(i).getFont();
1401                         if (font == 0) {
1402                                 int const c = params()
1403                                         .user_defined_bullet(i)
1404                                         .getCharacter();
1405                                 if (c == 16
1406                                    || c == 17
1407                                    || c == 25
1408                                    || c == 26
1409                                    || c == 31) {
1410                                         features.require("latexsym");
1411                                 }
1412                         } else if (font == 1) {
1413                                 features.require("amssymb");
1414                         } else if ((font >= 2 && font <= 5)) {
1415                                 features.require("pifont");
1416                         }
1417                 }
1418         }
1419
1420         if (lyxerr.debugging(Debug::LATEX)) {
1421                 features.showStruct();
1422         }
1423 }
1424
1425
1426 void Buffer::getLabelList(vector<docstring> & list) const
1427 {
1428         /// if this is a child document and the parent is already loaded
1429         /// Use the parent's list instead  [ale990407]
1430         Buffer const * tmp = masterBuffer();
1431         if (!tmp) {
1432                 lyxerr << "masterBuffer() failed!" << endl;
1433                 BOOST_ASSERT(tmp);
1434         }
1435         if (tmp != this) {
1436                 tmp->getLabelList(list);
1437                 return;
1438         }
1439
1440         loadChildDocuments();
1441
1442         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
1443                 it.nextInset()->getLabelList(*this, list);
1444 }
1445
1446
1447 void Buffer::updateBibfilesCache()
1448 {
1449         // if this is a child document and the parent is already loaded
1450         // update the parent's cache instead
1451         Buffer * tmp = masterBuffer();
1452         BOOST_ASSERT(tmp);
1453         if (tmp != this) {
1454                 tmp->updateBibfilesCache();
1455                 return;
1456         }
1457
1458         bibfilesCache_.clear();
1459         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
1460                 if (it->lyxCode() == BIBTEX_CODE) {
1461                         InsetBibtex const & inset =
1462                                 static_cast<InsetBibtex const &>(*it);
1463                         vector<FileName> const bibfiles = inset.getFiles(*this);
1464                         bibfilesCache_.insert(bibfilesCache_.end(),
1465                                 bibfiles.begin(),
1466                                 bibfiles.end());
1467                 } else if (it->lyxCode() == INCLUDE_CODE) {
1468                         InsetInclude & inset =
1469                                 static_cast<InsetInclude &>(*it);
1470                         inset.updateBibfilesCache(*this);
1471                         vector<FileName> const & bibfiles =
1472                                         inset.getBibfilesCache(*this);
1473                         bibfilesCache_.insert(bibfilesCache_.end(),
1474                                 bibfiles.begin(),
1475                                 bibfiles.end());
1476                 }
1477         }
1478 }
1479
1480
1481 vector<FileName> const & Buffer::getBibfilesCache() const
1482 {
1483         // if this is a child document and the parent is already loaded
1484         // use the parent's cache instead
1485         Buffer const * tmp = masterBuffer();
1486         BOOST_ASSERT(tmp);
1487         if (tmp != this)
1488                 return tmp->getBibfilesCache();
1489
1490         // We update the cache when first used instead of at loading time.
1491         if (bibfilesCache_.empty())
1492                 const_cast<Buffer *>(this)->updateBibfilesCache();
1493
1494         return bibfilesCache_;
1495 }
1496
1497
1498 bool Buffer::isDepClean(string const & name) const
1499 {
1500         DepClean::const_iterator const it = pimpl_->dep_clean.find(name);
1501         if (it == pimpl_->dep_clean.end())
1502                 return true;
1503         return it->second;
1504 }
1505
1506
1507 void Buffer::markDepClean(string const & name)
1508 {
1509         pimpl_->dep_clean[name] = true;
1510 }
1511
1512
1513 bool Buffer::dispatch(string const & command, bool * result)
1514 {
1515         return dispatch(lyxaction.lookupFunc(command), result);
1516 }
1517
1518
1519 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1520 {
1521         bool dispatched = true;
1522
1523         switch (func.action) {
1524                 case LFUN_BUFFER_EXPORT: {
1525                         bool const tmp = Exporter::Export(this, to_utf8(func.argument()), false);
1526                         if (result)
1527                                 *result = tmp;
1528                         break;
1529                 }
1530
1531                 default:
1532                         dispatched = false;
1533         }
1534         return dispatched;
1535 }
1536
1537
1538 void Buffer::changeLanguage(Language const * from, Language const * to)
1539 {
1540         BOOST_ASSERT(from);
1541         BOOST_ASSERT(to);
1542
1543         for_each(par_iterator_begin(),
1544                  par_iterator_end(),
1545                  bind(&Paragraph::changeLanguage, _1, params(), from, to));
1546 }
1547
1548
1549 bool Buffer::isMultiLingual() const
1550 {
1551         ParConstIterator end = par_iterator_end();
1552         for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1553                 if (it->isMultiLingual(params()))
1554                         return true;
1555
1556         return false;
1557 }
1558
1559
1560 ParIterator Buffer::getParFromID(int const id) const
1561 {
1562         ParConstIterator it = par_iterator_begin();
1563         ParConstIterator const end = par_iterator_end();
1564
1565         if (id < 0) {
1566                 // John says this is called with id == -1 from undo
1567                 lyxerr << "getParFromID(), id: " << id << endl;
1568                 return end;
1569         }
1570
1571         for (; it != end; ++it)
1572                 if (it->id() == id)
1573                         return it;
1574
1575         return end;
1576 }
1577
1578
1579 bool Buffer::hasParWithID(int const id) const
1580 {
1581         ParConstIterator const it = getParFromID(id);
1582         return it != par_iterator_end();
1583 }
1584
1585
1586 ParIterator Buffer::par_iterator_begin()
1587 {
1588         return lyx::par_iterator_begin(inset());
1589 }
1590
1591
1592 ParIterator Buffer::par_iterator_end()
1593 {
1594         return lyx::par_iterator_end(inset());
1595 }
1596
1597
1598 ParConstIterator Buffer::par_iterator_begin() const
1599 {
1600         return lyx::par_const_iterator_begin(inset());
1601 }
1602
1603
1604 ParConstIterator Buffer::par_iterator_end() const
1605 {
1606         return lyx::par_const_iterator_end(inset());
1607 }
1608
1609
1610 Language const * Buffer::language() const
1611 {
1612         return params().language;
1613 }
1614
1615
1616 docstring const Buffer::B_(string const & l10n) const
1617 {
1618         return params().B_(l10n);
1619 }
1620
1621
1622 bool Buffer::isClean() const
1623 {
1624         return pimpl_->lyx_clean;
1625 }
1626
1627
1628 bool Buffer::isBakClean() const
1629 {
1630         return pimpl_->bak_clean;
1631 }
1632
1633
1634 bool Buffer::isExternallyModified(CheckMethod method) const
1635 {
1636         BOOST_ASSERT(pimpl_->filename.exists());
1637         // if method == timestamp, check timestamp before checksum
1638         return (method == checksum_method 
1639                 || pimpl_->timestamp_ != pimpl_->filename.lastModified())
1640                 && pimpl_->checksum_ != sum(pimpl_->filename);
1641 }
1642
1643
1644 void Buffer::saveCheckSum(FileName const & file) const
1645 {
1646         if (file.exists()) {
1647                 pimpl_->timestamp_ = file.lastModified();
1648                 pimpl_->checksum_ = sum(file);
1649         } else {
1650                 // in the case of save to a new file.
1651                 pimpl_->timestamp_ = 0;
1652                 pimpl_->checksum_ = 0;
1653         }
1654 }
1655
1656
1657 void Buffer::markClean() const
1658 {
1659         if (!pimpl_->lyx_clean) {
1660                 pimpl_->lyx_clean = true;
1661                 updateTitles();
1662         }
1663         // if the .lyx file has been saved, we don't need an
1664         // autosave
1665         pimpl_->bak_clean = true;
1666 }
1667
1668
1669 void Buffer::markBakClean() const
1670 {
1671         pimpl_->bak_clean = true;
1672 }
1673
1674
1675 void Buffer::setUnnamed(bool flag)
1676 {
1677         pimpl_->unnamed = flag;
1678 }
1679
1680
1681 bool Buffer::isUnnamed() const
1682 {
1683         return pimpl_->unnamed;
1684 }
1685
1686
1687 // FIXME: this function should be moved to buffer_pimpl.C
1688 void Buffer::markDirty()
1689 {
1690         if (pimpl_->lyx_clean) {
1691                 pimpl_->lyx_clean = false;
1692                 updateTitles();
1693         }
1694         pimpl_->bak_clean = false;
1695
1696         DepClean::iterator it = pimpl_->dep_clean.begin();
1697         DepClean::const_iterator const end = pimpl_->dep_clean.end();
1698
1699         for (; it != end; ++it)
1700                 it->second = false;
1701 }
1702
1703
1704 string Buffer::absFileName() const
1705 {
1706         return pimpl_->filename.absFilename();
1707 }
1708
1709
1710 string const & Buffer::filePath() const
1711 {
1712         return params().filepath;
1713 }
1714
1715
1716 bool Buffer::isReadonly() const
1717 {
1718         return pimpl_->read_only;
1719 }
1720
1721
1722 void Buffer::setParentName(string const & name)
1723 {
1724         if (name == pimpl_->filename.absFilename())
1725                 // Avoids recursive include.
1726                 params().parentname.clear();
1727         else
1728                 params().parentname = name;
1729 }
1730
1731
1732 Buffer const * Buffer::masterBuffer() const
1733 {
1734         if (!params().parentname.empty()
1735             && theBufferList().exists(params().parentname)) {
1736                 Buffer const * buf = theBufferList().getBuffer(params().parentname);
1737                 //We need to check if the parent is us...
1738                 //FIXME RECURSIVE INCLUDE
1739                 //This is not sufficient, since recursive includes could be downstream.
1740                 if (buf && buf != this)
1741                         return buf->masterBuffer();
1742         }
1743
1744         return this;
1745 }
1746
1747
1748 Buffer * Buffer::masterBuffer()
1749 {
1750         if (!params().parentname.empty()
1751             && theBufferList().exists(params().parentname)) {
1752                 Buffer * buf = theBufferList().getBuffer(params().parentname);
1753                 //We need to check if the parent is us...
1754                 //FIXME RECURSIVE INCLUDE
1755                 //This is not sufficient, since recursive includes could be downstream.
1756                 if (buf && buf != this)
1757                         return buf->masterBuffer();
1758         }
1759
1760         return this;
1761 }
1762
1763
1764 MacroData const & Buffer::getMacro(docstring const & name) const
1765 {
1766         return pimpl_->macros.get(name);
1767 }
1768
1769
1770 bool Buffer::hasMacro(docstring const & name) const
1771 {
1772         return pimpl_->macros.has(name);
1773 }
1774
1775
1776 void Buffer::insertMacro(docstring const & name, MacroData const & data)
1777 {
1778         MacroTable::globalMacros().insert(name, data);
1779         pimpl_->macros.insert(name, data);
1780 }
1781
1782
1783 void Buffer::buildMacros()
1784 {
1785         // Start with global table.
1786         pimpl_->macros = MacroTable::globalMacros();
1787
1788         // Now add our own.
1789         ParagraphList const & pars = text().paragraphs();
1790         for (size_t i = 0, n = pars.size(); i != n; ++i) {
1791                 //lyxerr << "searching main par " << i
1792                 //      << " for macro definitions" << std::endl;
1793                 InsetList const & insets = pars[i].insetList();
1794                 InsetList::const_iterator it = insets.begin();
1795                 InsetList::const_iterator end = insets.end();
1796                 for ( ; it != end; ++it) {
1797                         //lyxerr << "found inset code " << it->inset->lyxCode() << std::endl;
1798                         if (it->inset->lyxCode() == MATHMACRO_CODE) {
1799                                 MathMacroTemplate const & mac
1800                                         = static_cast<MathMacroTemplate const &>(*it->inset);
1801                                 insertMacro(mac.name(), mac.asMacroData());
1802                         }
1803                 }
1804         }
1805 }
1806
1807
1808 void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
1809         InsetCode code)
1810 {
1811         //FIXME: This does not work for child documents yet.
1812         BOOST_ASSERT(code == CITE_CODE || code == REF_CODE);
1813         // Check if the label 'from' appears more than once
1814         vector<docstring> labels;
1815
1816         if (code == CITE_CODE) {
1817                 BiblioInfo keys;
1818                 keys.fillWithBibKeys(this);
1819                 BiblioInfo::const_iterator bit  = keys.begin();
1820                 BiblioInfo::const_iterator bend = keys.end();
1821
1822                 for (; bit != bend; ++bit)
1823                         // FIXME UNICODE
1824                         labels.push_back(bit->first);
1825         } else
1826                 getLabelList(labels);
1827
1828         if (std::count(labels.begin(), labels.end(), from) > 1)
1829                 return;
1830
1831         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
1832                 if (it->lyxCode() == code) {
1833                         InsetCommand & inset = static_cast<InsetCommand &>(*it);
1834                         inset.replaceContents(to_utf8(from), to_utf8(to));
1835                 }
1836         }
1837 }
1838
1839
1840 void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
1841         pit_type par_end, bool full_source)
1842 {
1843         OutputParams runparams(&params().encoding());
1844         runparams.nice = true;
1845         runparams.flavor = OutputParams::LATEX;
1846         runparams.linelen = lyxrc.plaintext_linelen;
1847         // No side effect of file copying and image conversion
1848         runparams.dryrun = true;
1849
1850         texrow().reset();
1851         if (full_source) {
1852                 os << "% " << _("Preview source code") << "\n\n";
1853                 texrow().newline();
1854                 texrow().newline();
1855                 if (isLatex())
1856                         writeLaTeXSource(os, filePath(), runparams, true, true);
1857                 else {
1858                         writeDocBookSource(os, absFileName(), runparams, false);
1859                 }
1860         } else {
1861                 runparams.par_begin = par_begin;
1862                 runparams.par_end = par_end;
1863                 if (par_begin + 1 == par_end)
1864                         os << "% "
1865                            << bformat(_("Preview source code for paragraph %1$d"), par_begin)
1866                            << "\n\n";
1867                 else
1868                         os << "% "
1869                            << bformat(_("Preview source code from paragraph %1$s to %2$s"),
1870                                         convert<docstring>(par_begin),
1871                                         convert<docstring>(par_end - 1))
1872                            << "\n\n";
1873                 texrow().newline();
1874                 texrow().newline();
1875                 // output paragraphs
1876                 if (isLatex()) {
1877                         latexParagraphs(*this, paragraphs(), os, texrow(), runparams);
1878                 } else {
1879                         // DocBook
1880                         docbookParagraphs(paragraphs(), *this, os, runparams);
1881                 }
1882         }
1883 }
1884
1885
1886 ErrorList const & Buffer::errorList(string const & type) const
1887 {
1888         static ErrorList const emptyErrorList;
1889         std::map<string, ErrorList>::const_iterator I = pimpl_->errorLists.find(type);
1890         if (I == pimpl_->errorLists.end())
1891                 return emptyErrorList;
1892
1893         return I->second;
1894 }
1895
1896
1897 ErrorList & Buffer::errorList(string const & type)
1898 {
1899         return pimpl_->errorLists[type];
1900 }
1901
1902
1903 void Buffer::structureChanged() const
1904 {
1905         if (gui_)
1906                 gui_->structureChanged();
1907 }
1908
1909
1910 void Buffer::embeddingChanged() const
1911 {
1912         if (gui_)
1913                 gui_->embeddingChanged();
1914 }
1915
1916
1917 void Buffer::errors(std::string const & err) const
1918 {
1919         if (gui_)
1920                 gui_->errors(err);
1921 }
1922
1923
1924 void Buffer::message(docstring const & msg) const
1925 {
1926         if (gui_)
1927                 gui_->message(msg);
1928 }
1929
1930
1931 void Buffer::busy(bool on) const
1932 {
1933         if (gui_)
1934                 gui_->busy(on);
1935 }
1936
1937
1938 void Buffer::readonly(bool on) const
1939 {
1940         if (gui_)
1941                 gui_->readonly(on);
1942 }
1943
1944
1945 void Buffer::updateTitles() const
1946 {
1947         if (gui_)
1948                 gui_->updateTitles();
1949 }
1950
1951
1952 void Buffer::resetAutosaveTimers() const
1953 {
1954         if (gui_)
1955                 gui_->resetAutosaveTimers();
1956 }
1957
1958
1959 void Buffer::setGuiDelegate(frontend::GuiBufferDelegate * gui)
1960 {
1961         gui_ = gui;
1962 }
1963
1964
1965
1966 namespace {
1967
1968 class AutoSaveBuffer : public support::ForkedProcess {
1969 public:
1970         ///
1971         AutoSaveBuffer(Buffer const & buffer, FileName const & fname)
1972                 : buffer_(buffer), fname_(fname) {}
1973         ///
1974         virtual boost::shared_ptr<ForkedProcess> clone() const
1975         {
1976                 return boost::shared_ptr<ForkedProcess>(new AutoSaveBuffer(*this));
1977         }
1978         ///
1979         int start()
1980         {
1981                 command_ = to_utf8(bformat(_("Auto-saving %1$s"), 
1982                                                  from_utf8(fname_.absFilename())));
1983                 return run(DontWait);
1984         }
1985 private:
1986         ///
1987         virtual int generateChild();
1988         ///
1989         Buffer const & buffer_;
1990         FileName fname_;
1991 };
1992
1993
1994 #if !defined (HAVE_FORK)
1995 # define fork() -1
1996 #endif
1997
1998 int AutoSaveBuffer::generateChild()
1999 {
2000         // tmp_ret will be located (usually) in /tmp
2001         // will that be a problem?
2002         pid_t const pid = fork();
2003         // If you want to debug the autosave
2004         // you should set pid to -1, and comment out the fork.
2005         if (pid == 0 || pid == -1) {
2006                 // pid = -1 signifies that lyx was unable
2007                 // to fork. But we will do the save
2008                 // anyway.
2009                 bool failed = false;
2010
2011                 FileName const tmp_ret(tempName(FileName(), "lyxauto"));
2012                 if (!tmp_ret.empty()) {
2013                         buffer_.writeFile(tmp_ret);
2014                         // assume successful write of tmp_ret
2015                         if (!rename(tmp_ret, fname_)) {
2016                                 failed = true;
2017                                 // most likely couldn't move between
2018                                 // filesystems unless write of tmp_ret
2019                                 // failed so remove tmp file (if it
2020                                 // exists)
2021                                 unlink(tmp_ret);
2022                         }
2023                 } else {
2024                         failed = true;
2025                 }
2026
2027                 if (failed) {
2028                         // failed to write/rename tmp_ret so try writing direct
2029                         if (!buffer_.writeFile(fname_)) {
2030                                 // It is dangerous to do this in the child,
2031                                 // but safe in the parent, so...
2032                                 if (pid == -1) // emit message signal.
2033                                         buffer_.message(_("Autosave failed!"));
2034                         }
2035                 }
2036                 if (pid == 0) { // we are the child so...
2037                         _exit(0);
2038                 }
2039         }
2040         return pid;
2041 }
2042
2043 } // namespace anon
2044
2045
2046 // Perfect target for a thread...
2047 void Buffer::autoSave() const
2048 {
2049         if (isBakClean() || isReadonly()) {
2050                 // We don't save now, but we'll try again later
2051                 resetAutosaveTimers();
2052                 return;
2053         }
2054
2055         // emit message signal.
2056         message(_("Autosaving current document..."));
2057
2058         // create autosave filename
2059         string fname = filePath();
2060         fname += '#';
2061         fname += onlyFilename(absFileName());
2062         fname += '#';
2063
2064         AutoSaveBuffer autosave(*this, FileName(fname));
2065         autosave.start();
2066
2067         markBakClean();
2068         resetAutosaveTimers();
2069 }
2070
2071
2072 /** Write a buffer to a new file name and rename the buffer
2073     according to the new file name.
2074
2075     This function is e.g. used by menu callbacks and
2076     LFUN_BUFFER_WRITE_AS.
2077
2078     If 'newname' is empty (the default), the user is asked via a
2079     dialog for the buffer's new name and location.
2080
2081     If 'newname' is non-empty and has an absolute path, that is used.
2082     Otherwise the base directory of the buffer is used as the base
2083     for any relative path in 'newname'.
2084 */
2085
2086 bool Buffer::writeAs(string const & newname)
2087 {
2088         string fname = absFileName();
2089         string const oldname = fname;
2090
2091         if (newname.empty()) {  /// No argument? Ask user through dialog
2092
2093                 // FIXME UNICODE
2094                 FileDialog fileDlg(_("Choose a filename to save document as"),
2095                                    LFUN_BUFFER_WRITE_AS,
2096                                    make_pair(_("Documents|#o#O"), 
2097                                              from_utf8(lyxrc.document_path)),
2098                                    make_pair(_("Templates|#T#t"), 
2099                                              from_utf8(lyxrc.template_path)));
2100
2101                 if (!support::isLyXFilename(fname))
2102                         fname += ".lyx";
2103
2104                 support::FileFilterList const filter(_("LyX Documents (*.lyx)"));
2105
2106                 FileDialog::Result result =
2107                         fileDlg.save(from_utf8(onlyPath(fname)),
2108                                      filter,
2109                                      from_utf8(onlyFilename(fname)));
2110
2111                 if (result.first == FileDialog::Later)
2112                         return false;
2113
2114                 fname = to_utf8(result.second);
2115
2116                 if (fname.empty())
2117                         return false;
2118
2119                 // Make sure the absolute filename ends with appropriate suffix
2120                 fname = makeAbsPath(fname).absFilename();
2121                 if (!support::isLyXFilename(fname))
2122                         fname += ".lyx";
2123
2124         } else 
2125                 fname = makeAbsPath(newname, onlyPath(oldname)).absFilename();
2126
2127         if (FileName(fname).exists()) {
2128                 docstring const file = makeDisplayPath(fname, 30);
2129                 docstring text = bformat(_("The document %1$s already "
2130                                            "exists.\n\nDo you want to "
2131                                            "overwrite that document?"), 
2132                                          file);
2133                 int const ret = Alert::prompt(_("Overwrite document?"),
2134                         text, 0, 1, _("&Overwrite"), _("&Cancel"));
2135
2136                 if (ret == 1)
2137                         return false;
2138         }
2139
2140         // Ok, change the name of the buffer
2141         setFileName(fname);
2142         markDirty();
2143         bool unnamed = isUnnamed();
2144         setUnnamed(false);
2145         saveCheckSum(FileName(fname));
2146
2147         if (!menuWrite()) {
2148                 setFileName(oldname);
2149                 setUnnamed(unnamed);
2150                 saveCheckSum(FileName(oldname));
2151                 return false;
2152         }
2153
2154         removeAutosaveFile(oldname);
2155         return true;
2156 }
2157
2158
2159 bool Buffer::menuWrite()
2160 {
2161         if (save()) {
2162                 LyX::ref().session().lastFiles().add(FileName(absFileName()));
2163                 return true;
2164         }
2165
2166         // FIXME: we don't tell the user *WHY* the save failed !!
2167
2168         docstring const file = makeDisplayPath(absFileName(), 30);
2169
2170         docstring text = bformat(_("The document %1$s could not be saved.\n\n"
2171                                    "Do you want to rename the document and "
2172                                    "try again?"), file);
2173         int const ret = Alert::prompt(_("Rename and save?"),
2174                 text, 0, 1, _("&Rename"), _("&Cancel"));
2175
2176         if (ret != 0)
2177                 return false;
2178
2179         return writeAs();
2180 }
2181
2182
2183 void Buffer::loadChildDocuments() const
2184 {
2185         bool parse_error = false;
2186                 
2187         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
2188                 if (it->lyxCode() != INCLUDE_CODE)
2189                         continue;
2190                 InsetInclude const & inset = static_cast<InsetInclude const &>(*it);
2191                 InsetCommandParams const & ip = inset.params();
2192                 Buffer * child = loadIfNeeded(*this, ip);
2193                 if (!child)
2194                         continue;
2195                 parse_error |= !child->errorList("Parse").empty();
2196                 child->loadChildDocuments();
2197         }
2198
2199         if (use_gui && masterBuffer() == this)
2200                 updateLabels(*this);
2201 }
2202
2203
2204 string Buffer::bufferFormat() const
2205 {
2206         if (isDocBook())
2207                 return "docbook";
2208         if (isLiterate())
2209                 return "literate";
2210         return "latex";
2211 }
2212
2213
2214 } // namespace lyx