]> git.lyx.org Git - lyx.git/blob - src/Buffer.cpp
Add a comment.
[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  * \author Stefan Schimanski
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Buffer.h"
15
16 #include "Author.h"
17 #include "LayoutFile.h"
18 #include "BiblioInfo.h"
19 #include "BranchList.h"
20 #include "buffer_funcs.h"
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "Bullet.h"
24 #include "Chktex.h"
25 #include "Converter.h"
26 #include "Counters.h"
27 #include "DocIterator.h"
28 #include "Encoding.h"
29 #include "ErrorList.h"
30 #include "Exporter.h"
31 #include "Format.h"
32 #include "FuncRequest.h"
33 #include "InsetIterator.h"
34 #include "InsetList.h"
35 #include "Language.h"
36 #include "LaTeXFeatures.h"
37 #include "LaTeX.h"
38 #include "Layout.h"
39 #include "Lexer.h"
40 #include "LyXAction.h"
41 #include "LyX.h"
42 #include "LyXRC.h"
43 #include "LyXVC.h"
44 #include "output_docbook.h"
45 #include "output.h"
46 #include "output_latex.h"
47 #include "output_plaintext.h"
48 #include "paragraph_funcs.h"
49 #include "Paragraph.h"
50 #include "ParagraphParameters.h"
51 #include "ParIterator.h"
52 #include "PDFOptions.h"
53 #include "sgml.h"
54 #include "TexRow.h"
55 #include "TexStream.h"
56 #include "Text.h"
57 #include "TextClass.h"
58 #include "TocBackend.h"
59 #include "Undo.h"
60 #include "VCBackend.h"
61 #include "version.h"
62 #include "WordList.h"
63
64 #include "insets/InsetBibitem.h"
65 #include "insets/InsetBibtex.h"
66 #include "insets/InsetInclude.h"
67 #include "insets/InsetText.h"
68
69 #include "mathed/MacroTable.h"
70 #include "mathed/MathMacroTemplate.h"
71 #include "mathed/MathSupport.h"
72
73 #include "frontends/alert.h"
74 #include "frontends/Delegates.h"
75 #include "frontends/WorkAreaManager.h"
76
77 #include "graphics/Previews.h"
78
79 #include "support/lassert.h"
80 #include "support/convert.h"
81 #include "support/debug.h"
82 #include "support/ExceptionMessage.h"
83 #include "support/FileName.h"
84 #include "support/FileNameList.h"
85 #include "support/filetools.h"
86 #include "support/ForkedCalls.h"
87 #include "support/gettext.h"
88 #include "support/gzstream.h"
89 #include "support/lstrings.h"
90 #include "support/lyxalgo.h"
91 #include "support/os.h"
92 #include "support/Package.h"
93 #include "support/Path.h"
94 #include "support/textutils.h"
95 #include "support/types.h"
96
97 #include <boost/bind.hpp>
98 #include <boost/shared_ptr.hpp>
99
100 #include <algorithm>
101 #include <fstream>
102 #include <iomanip>
103 #include <map>
104 #include <set>
105 #include <sstream>
106 #include <stack>
107 #include <vector>
108
109 using namespace std;
110 using namespace lyx::support;
111
112 namespace lyx {
113
114 namespace Alert = frontend::Alert;
115 namespace os = support::os;
116
117 namespace {
118
119 // Do not remove the comment below, so we get merge conflict in
120 // independent branches. Instead add your own.
121 int const LYX_FORMAT = 348;  // uwestoehr: add support for \*phantom
122
123 typedef map<string, bool> DepClean;
124 typedef map<docstring, pair<InsetLabel const *, Buffer::References> > RefCache;
125
126 } // namespace anon
127
128 class BufferSet : public std::set<Buffer const *> {};
129
130 class Buffer::Impl
131 {
132 public:
133         Impl(Buffer & parent, FileName const & file, bool readonly);
134
135         ~Impl()
136         {
137                 if (wa_) {
138                         wa_->closeAll();
139                         delete wa_;
140                 }
141                 delete inset;
142         }
143
144         BufferParams params;
145         LyXVC lyxvc;
146         FileName temppath;
147         mutable TexRow texrow;
148
149         /// need to regenerate .tex?
150         DepClean dep_clean;
151
152         /// is save needed?
153         mutable bool lyx_clean;
154
155         /// is autosave needed?
156         mutable bool bak_clean;
157
158         /// is this a unnamed file (New...)?
159         bool unnamed;
160
161         /// buffer is r/o
162         bool read_only;
163
164         /// name of the file the buffer is associated with.
165         FileName filename;
166
167         /** Set to true only when the file is fully loaded.
168          *  Used to prevent the premature generation of previews
169          *  and by the citation inset.
170          */
171         bool file_fully_loaded;
172
173         ///
174         mutable TocBackend toc_backend;
175
176         /// macro tables
177         typedef pair<DocIterator, MacroData> ScopeMacro;
178         typedef map<DocIterator, ScopeMacro> PositionScopeMacroMap;
179         typedef map<docstring, PositionScopeMacroMap> NamePositionScopeMacroMap;
180         /// map from the macro name to the position map,
181         /// which maps the macro definition position to the scope and the MacroData.
182         NamePositionScopeMacroMap macros;
183         bool macro_lock;
184
185         /// positions of child buffers in the buffer
186         typedef map<Buffer const * const, DocIterator> BufferPositionMap;
187         typedef pair<DocIterator, Buffer const *> ScopeBuffer;
188         typedef map<DocIterator, ScopeBuffer> PositionScopeBufferMap;
189         /// position of children buffers in this buffer
190         BufferPositionMap children_positions;
191         /// map from children inclusion positions to their scope and their buffer
192         PositionScopeBufferMap position_to_children;
193
194         /// Container for all sort of Buffer dependant errors.
195         map<string, ErrorList> errorLists;
196
197         /// timestamp and checksum used to test if the file has been externally
198         /// modified. (Used to properly enable 'File->Revert to saved', bug 4114).
199         time_t timestamp_;
200         unsigned long checksum_;
201
202         ///
203         frontend::WorkAreaManager * wa_;
204
205         ///
206         Undo undo_;
207
208         /// A cache for the bibfiles (including bibfiles of loaded child
209         /// documents), needed for appropriate update of natbib labels.
210         mutable support::FileNameList bibfilesCache_;
211
212         // FIXME The caching mechanism could be improved. At present, we have a
213         // cache for each Buffer, that caches all the bibliography info for that
214         // Buffer. A more efficient solution would be to have a global cache per
215         // file, and then to construct the Buffer's bibinfo from that.
216         /// A cache for bibliography info
217         mutable BiblioInfo bibinfo_;
218         /// whether the bibinfo cache is valid
219         bool bibinfoCacheValid_;
220         /// Cache of timestamps of .bib files
221         map<FileName, time_t> bibfileStatus_;
222
223         mutable RefCache ref_cache_;
224
225         /// our Text that should be wrapped in an InsetText
226         InsetText * inset;
227
228         /// This is here to force the test to be done whenever parent_buffer
229         /// is accessed.
230         Buffer const * parent() const { 
231                 // if parent_buffer is not loaded, then it has been unloaded,
232                 // which means that parent_buffer is an invalid pointer. So we
233                 // set it to null in that case.
234                 if (!theBufferList().isLoaded(parent_buffer))
235                         parent_buffer = 0;
236                 return parent_buffer; 
237         }
238         ///
239         void setParent(Buffer const * pb) { parent_buffer = pb; }
240 private:
241         /// So we can force access via the accessors.
242         mutable Buffer const * parent_buffer;
243 };
244
245
246 /// Creates the per buffer temporary directory
247 static FileName createBufferTmpDir()
248 {
249         static int count;
250         // We are in our own directory.  Why bother to mangle name?
251         // In fact I wrote this code to circumvent a problematic behaviour
252         // (bug?) of EMX mkstemp().
253         FileName tmpfl(package().temp_dir().absFilename() + "/lyx_tmpbuf" +
254                 convert<string>(count++));
255
256         if (!tmpfl.createDirectory(0777)) {
257                 throw ExceptionMessage(WarningException, _("Disk Error: "), bformat(
258                         _("LyX could not create the temporary directory '%1$s' (Disk is full maybe?)"),
259                         from_utf8(tmpfl.absFilename())));
260         }
261         return tmpfl;
262 }
263
264
265 Buffer::Impl::Impl(Buffer & parent, FileName const & file, bool readonly_)
266         : lyx_clean(true), bak_clean(true), unnamed(false),
267           read_only(readonly_), filename(file), file_fully_loaded(false),
268           toc_backend(&parent), macro_lock(false), timestamp_(0),
269           checksum_(0), wa_(0), undo_(parent), bibinfoCacheValid_(false),
270           parent_buffer(0)
271 {
272         temppath = createBufferTmpDir();
273         lyxvc.setBuffer(&parent);
274         if (use_gui)
275                 wa_ = new frontend::WorkAreaManager;
276 }
277
278
279 Buffer::Buffer(string const & file, bool readonly)
280         : d(new Impl(*this, FileName(file), readonly)), gui_(0)
281 {
282         LYXERR(Debug::INFO, "Buffer::Buffer()");
283
284         d->inset = new InsetText(*this);
285         d->inset->setAutoBreakRows(true);
286         d->inset->getText(0)->setMacrocontextPosition(par_iterator_begin());
287 }
288
289
290 Buffer::~Buffer()
291 {
292         LYXERR(Debug::INFO, "Buffer::~Buffer()");
293         // here the buffer should take care that it is
294         // saved properly, before it goes into the void.
295
296         // GuiView already destroyed
297         gui_ = 0;
298
299         if (d->unnamed && d->filename.extension() == "internal") {
300                 // No need to do additional cleanups for internal buffer.
301                 delete d;
302                 return;
303         }
304
305         // loop over children
306         Impl::BufferPositionMap::iterator it = d->children_positions.begin();
307         Impl::BufferPositionMap::iterator end = d->children_positions.end();
308         for (; it != end; ++it) {
309                 Buffer * child = const_cast<Buffer *>(it->first);
310                 // The child buffer might have been closed already.
311                 if (theBufferList().isLoaded(child))
312                         theBufferList().releaseChild(this, child);
313         }
314
315         // clear references to children in macro tables
316         d->children_positions.clear();
317         d->position_to_children.clear();
318
319         if (!d->temppath.destroyDirectory()) {
320                 Alert::warning(_("Could not remove temporary directory"),
321                         bformat(_("Could not remove the temporary directory %1$s"),
322                         from_utf8(d->temppath.absFilename())));
323         }
324
325         // Remove any previewed LaTeX snippets associated with this buffer.
326         thePreviews().removeLoader(*this);
327
328         delete d;
329 }
330
331
332 void Buffer::changed() const
333 {
334         if (d->wa_)
335                 d->wa_->redrawAll();
336 }
337
338
339 frontend::WorkAreaManager & Buffer::workAreaManager() const
340 {
341         LASSERT(d->wa_, /**/);
342         return *d->wa_;
343 }
344
345
346 Text & Buffer::text() const
347 {
348         return d->inset->text();
349 }
350
351
352 Inset & Buffer::inset() const
353 {
354         return *d->inset;
355 }
356
357
358 BufferParams & Buffer::params()
359 {
360         return d->params;
361 }
362
363
364 BufferParams const & Buffer::params() const
365 {
366         return d->params;
367 }
368
369
370 ParagraphList & Buffer::paragraphs()
371 {
372         return text().paragraphs();
373 }
374
375
376 ParagraphList const & Buffer::paragraphs() const
377 {
378         return text().paragraphs();
379 }
380
381
382 LyXVC & Buffer::lyxvc()
383 {
384         return d->lyxvc;
385 }
386
387
388 LyXVC const & Buffer::lyxvc() const
389 {
390         return d->lyxvc;
391 }
392
393
394 string const Buffer::temppath() const
395 {
396         return d->temppath.absFilename();
397 }
398
399
400 TexRow & Buffer::texrow()
401 {
402         return d->texrow;
403 }
404
405
406 TexRow const & Buffer::texrow() const
407 {
408         return d->texrow;
409 }
410
411
412 TocBackend & Buffer::tocBackend() const
413 {
414         return d->toc_backend;
415 }
416
417
418 Undo & Buffer::undo()
419 {
420         return d->undo_;
421 }
422
423
424 string Buffer::latexName(bool const no_path) const
425 {
426         FileName latex_name = makeLatexName(d->filename);
427         return no_path ? latex_name.onlyFileName()
428                 : latex_name.absFilename();
429 }
430
431
432 string Buffer::logName(LogType * type) const
433 {
434         string const filename = latexName(false);
435
436         if (filename.empty()) {
437                 if (type)
438                         *type = latexlog;
439                 return string();
440         }
441
442         string const path = temppath();
443
444         FileName const fname(addName(temppath(),
445                                      onlyFilename(changeExtension(filename,
446                                                                   ".log"))));
447         FileName const bname(
448                 addName(path, onlyFilename(
449                         changeExtension(filename,
450                                         formats.extension("literate") + ".out"))));
451
452         // If no Latex log or Build log is newer, show Build log
453
454         if (bname.exists() &&
455             (!fname.exists() || fname.lastModified() < bname.lastModified())) {
456                 LYXERR(Debug::FILES, "Log name calculated as: " << bname);
457                 if (type)
458                         *type = buildlog;
459                 return bname.absFilename();
460         }
461         LYXERR(Debug::FILES, "Log name calculated as: " << fname);
462         if (type)
463                         *type = latexlog;
464         return fname.absFilename();
465 }
466
467
468 void Buffer::setReadonly(bool const flag)
469 {
470         if (d->read_only != flag) {
471                 d->read_only = flag;
472                 setReadOnly(flag);
473         }
474 }
475
476
477 void Buffer::setFileName(string const & newfile)
478 {
479         d->filename = makeAbsPath(newfile);
480         setReadonly(d->filename.isReadOnly());
481         updateTitles();
482 }
483
484
485 int Buffer::readHeader(Lexer & lex)
486 {
487         int unknown_tokens = 0;
488         int line = -1;
489         int begin_header_line = -1;
490
491         // Initialize parameters that may be/go lacking in header:
492         params().branchlist().clear();
493         params().preamble.erase();
494         params().options.erase();
495         params().master.erase();
496         params().float_placement.erase();
497         params().paperwidth.erase();
498         params().paperheight.erase();
499         params().leftmargin.erase();
500         params().rightmargin.erase();
501         params().topmargin.erase();
502         params().bottommargin.erase();
503         params().headheight.erase();
504         params().headsep.erase();
505         params().footskip.erase();
506         params().columnsep.erase();
507         params().fontsCJK.erase();
508         params().listings_params.clear();
509         params().clearLayoutModules();
510         params().clearRemovedModules();
511         params().pdfoptions().clear();
512
513         for (int i = 0; i < 4; ++i) {
514                 params().user_defined_bullet(i) = ITEMIZE_DEFAULTS[i];
515                 params().temp_bullet(i) = ITEMIZE_DEFAULTS[i];
516         }
517
518         ErrorList & errorList = d->errorLists["Parse"];
519
520         while (lex.isOK()) {
521                 string token;
522                 lex >> token;
523
524                 if (token.empty())
525                         continue;
526
527                 if (token == "\\end_header")
528                         break;
529
530                 ++line;
531                 if (token == "\\begin_header") {
532                         begin_header_line = line;
533                         continue;
534                 }
535
536                 LYXERR(Debug::PARSER, "Handling document header token: `"
537                                       << token << '\'');
538
539                 string unknown = params().readToken(lex, token, d->filename.onlyPath());
540                 if (!unknown.empty()) {
541                         if (unknown[0] != '\\' && token == "\\textclass") {
542                                 Alert::warning(_("Unknown document class"),
543                        bformat(_("Using the default document class, because the "
544                                               "class %1$s is unknown."), from_utf8(unknown)));
545                         } else {
546                                 ++unknown_tokens;
547                                 docstring const s = bformat(_("Unknown token: "
548                                                                         "%1$s %2$s\n"),
549                                                          from_utf8(token),
550                                                          lex.getDocString());
551                                 errorList.push_back(ErrorItem(_("Document header error"),
552                                         s, -1, 0, 0));
553                         }
554                 }
555         }
556         if (begin_header_line) {
557                 docstring const s = _("\\begin_header is missing");
558                 errorList.push_back(ErrorItem(_("Document header error"),
559                         s, -1, 0, 0));
560         }
561
562         params().makeDocumentClass();
563
564         return unknown_tokens;
565 }
566
567
568 // Uwe C. Schroeder
569 // changed to be public and have one parameter
570 // Returns true if "\end_document" is not read (Asger)
571 bool Buffer::readDocument(Lexer & lex)
572 {
573         ErrorList & errorList = d->errorLists["Parse"];
574         errorList.clear();
575
576         if (!lex.checkFor("\\begin_document")) {
577                 docstring const s = _("\\begin_document is missing");
578                 errorList.push_back(ErrorItem(_("Document header error"),
579                         s, -1, 0, 0));
580         }
581
582         // we are reading in a brand new document
583         LASSERT(paragraphs().empty(), /**/);
584
585         readHeader(lex);
586
587         if (params().outputChanges) {
588                 bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
589                 bool xcolorulem = LaTeXFeatures::isAvailable("ulem") &&
590                                   LaTeXFeatures::isAvailable("xcolor");
591
592                 if (!dvipost && !xcolorulem) {
593                         Alert::warning(_("Changes not shown in LaTeX output"),
594                                        _("Changes will not be highlighted in LaTeX output, "
595                                          "because neither dvipost nor xcolor/ulem are installed.\n"
596                                          "Please install these packages or redefine "
597                                          "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
598                 } else if (!xcolorulem) {
599                         Alert::warning(_("Changes not shown in LaTeX output"),
600                                        _("Changes will not be highlighted in LaTeX output "
601                                          "when using pdflatex, because xcolor and ulem are not installed.\n"
602                                          "Please install both packages or redefine "
603                                          "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
604                 }
605         }
606
607         if (!params().master.empty()) {
608                 FileName const master_file = makeAbsPath(params().master,
609                            onlyPath(absFileName()));
610                 if (isLyXFilename(master_file.absFilename())) {
611                         Buffer * master = 
612                                 checkAndLoadLyXFile(master_file, true);
613                         if (master) {
614                                 // necessary e.g. after a reload
615                                 // to re-register the child (bug 5873)
616                                 // FIXME: clean up updateMacros (here, only
617                                 // child registering is needed).
618                                 master->updateMacros();
619                                 // set master as master buffer, but only
620                                 // if we are a real child
621                                 if (master->isChild(this))
622                                         setParent(master);
623                                 // if the master is not fully loaded
624                                 // it is probably just loading this
625                                 // child. No warning needed then.
626                                 else if (master->isFullyLoaded())
627                                         LYXERR0("The master '"
628                                                 << params().master
629                                                 << "' assigned to this document ("
630                                                 << absFileName()
631                                                 << ") does not include "
632                                                 "this document. Ignoring the master assignment.");
633                         }
634                 }
635         }
636
637         // read main text
638         bool const res = text().read(*this, lex, errorList, d->inset);
639
640         updateMacros();
641         updateMacroInstances();
642         return res;
643 }
644
645
646 // needed to insert the selection
647 void Buffer::insertStringAsLines(ParagraphList & pars,
648         pit_type & pit, pos_type & pos,
649         Font const & fn, docstring const & str, bool autobreakrows)
650 {
651         Font font = fn;
652
653         // insert the string, don't insert doublespace
654         bool space_inserted = true;
655         for (docstring::const_iterator cit = str.begin();
656             cit != str.end(); ++cit) {
657                 Paragraph & par = pars[pit];
658                 if (*cit == '\n') {
659                         if (autobreakrows && (!par.empty() || par.allowEmpty())) {
660                                 breakParagraph(params(), pars, pit, pos,
661                                                par.layout().isEnvironment());
662                                 ++pit;
663                                 pos = 0;
664                                 space_inserted = true;
665                         } else {
666                                 continue;
667                         }
668                         // do not insert consecutive spaces if !free_spacing
669                 } else if ((*cit == ' ' || *cit == '\t') &&
670                            space_inserted && !par.isFreeSpacing()) {
671                         continue;
672                 } else if (*cit == '\t') {
673                         if (!par.isFreeSpacing()) {
674                                 // tabs are like spaces here
675                                 par.insertChar(pos, ' ', font, params().trackChanges);
676                                 ++pos;
677                                 space_inserted = true;
678                         } else {
679                                 par.insertChar(pos, *cit, font, params().trackChanges);
680                                 ++pos;
681                                 space_inserted = true;
682                         }
683                 } else if (!isPrintable(*cit)) {
684                         // Ignore unprintables
685                         continue;
686                 } else {
687                         // just insert the character
688                         par.insertChar(pos, *cit, font, params().trackChanges);
689                         ++pos;
690                         space_inserted = (*cit == ' ');
691                 }
692
693         }
694 }
695
696
697 bool Buffer::readString(string const & s)
698 {
699         params().compressed = false;
700
701         // remove dummy empty par
702         paragraphs().clear();
703         Lexer lex;
704         istringstream is(s);
705         lex.setStream(is);
706         FileName const name = FileName::tempName("Buffer_readString");
707         switch (readFile(lex, name, true)) {
708         case failure:
709                 return false;
710         case wrongversion: {
711                 // We need to call lyx2lyx, so write the input to a file
712                 ofstream os(name.toFilesystemEncoding().c_str());
713                 os << s;
714                 os.close();
715                 return readFile(name);
716         }
717         case success:
718                 break;
719         }
720
721         return true;
722 }
723
724
725 bool Buffer::readFile(FileName const & filename)
726 {
727         FileName fname(filename);
728
729         params().compressed = fname.isZippedFile();
730
731         // remove dummy empty par
732         paragraphs().clear();
733         Lexer lex;
734         lex.setFile(fname);
735         if (readFile(lex, fname) != success)
736                 return false;
737
738         return true;
739 }
740
741
742 bool Buffer::isFullyLoaded() const
743 {
744         return d->file_fully_loaded;
745 }
746
747
748 void Buffer::setFullyLoaded(bool value)
749 {
750         d->file_fully_loaded = value;
751 }
752
753
754 Buffer::ReadStatus Buffer::readFile(Lexer & lex, FileName const & filename,
755                 bool fromstring)
756 {
757         LASSERT(!filename.empty(), /**/);
758
759         // the first (non-comment) token _must_ be...
760         if (!lex.checkFor("\\lyxformat")) {
761                 Alert::error(_("Document format failure"),
762                              bformat(_("%1$s is not a readable LyX document."),
763                                        from_utf8(filename.absFilename())));
764                 return failure;
765         }
766
767         string tmp_format;
768         lex >> tmp_format;
769         //lyxerr << "LyX Format: `" << tmp_format << '\'' << endl;
770         // if present remove ".," from string.
771         size_t dot = tmp_format.find_first_of(".,");
772         //lyxerr << "           dot found at " << dot << endl;
773         if (dot != string::npos)
774                         tmp_format.erase(dot, 1);
775         int const file_format = convert<int>(tmp_format);
776         //lyxerr << "format: " << file_format << endl;
777
778         // save timestamp and checksum of the original disk file, making sure
779         // to not overwrite them with those of the file created in the tempdir
780         // when it has to be converted to the current format.
781         if (!d->checksum_) {
782                 // Save the timestamp and checksum of disk file. If filename is an
783                 // emergency file, save the timestamp and checksum of the original lyx file
784                 // because isExternallyModified will check for this file. (BUG4193)
785                 string diskfile = filename.absFilename();
786                 if (suffixIs(diskfile, ".emergency"))
787                         diskfile = diskfile.substr(0, diskfile.size() - 10);
788                 saveCheckSum(FileName(diskfile));
789         }
790
791         if (file_format != LYX_FORMAT) {
792
793                 if (fromstring)
794                         // lyx2lyx would fail
795                         return wrongversion;
796
797                 FileName const tmpfile = FileName::tempName("Buffer_readFile");
798                 if (tmpfile.empty()) {
799                         Alert::error(_("Conversion failed"),
800                                      bformat(_("%1$s is from a different"
801                                               " version of LyX, but a temporary"
802                                               " file for converting it could"
803                                               " not be created."),
804                                               from_utf8(filename.absFilename())));
805                         return failure;
806                 }
807                 FileName const lyx2lyx = libFileSearch("lyx2lyx", "lyx2lyx");
808                 if (lyx2lyx.empty()) {
809                         Alert::error(_("Conversion script not found"),
810                                      bformat(_("%1$s is from a different"
811                                                " version of LyX, but the"
812                                                " conversion script lyx2lyx"
813                                                " could not be found."),
814                                                from_utf8(filename.absFilename())));
815                         return failure;
816                 }
817                 ostringstream command;
818                 command << os::python()
819                         << ' ' << quoteName(lyx2lyx.toFilesystemEncoding())
820                         << " -t " << convert<string>(LYX_FORMAT)
821                         << " -o " << quoteName(tmpfile.toFilesystemEncoding())
822                         << ' ' << quoteName(filename.toFilesystemEncoding());
823                 string const command_str = command.str();
824
825                 LYXERR(Debug::INFO, "Running '" << command_str << '\'');
826
827                 cmd_ret const ret = runCommand(command_str);
828                 if (ret.first != 0) {
829                         Alert::error(_("Conversion script failed"),
830                                      bformat(_("%1$s is from a different version"
831                                               " of LyX, but the lyx2lyx script"
832                                               " failed to convert it."),
833                                               from_utf8(filename.absFilename())));
834                         return failure;
835                 } else {
836                         bool const ret = readFile(tmpfile);
837                         // Do stuff with tmpfile name and buffer name here.
838                         return ret ? success : failure;
839                 }
840
841         }
842
843         if (readDocument(lex)) {
844                 Alert::error(_("Document format failure"),
845                              bformat(_("%1$s ended unexpectedly, which means"
846                                                     " that it is probably corrupted."),
847                                        from_utf8(filename.absFilename())));
848         }
849
850         d->file_fully_loaded = true;
851         return success;
852 }
853
854
855 // Should probably be moved to somewhere else: BufferView? LyXView?
856 bool Buffer::save() const
857 {
858         // We don't need autosaves in the immediate future. (Asger)
859         resetAutosaveTimers();
860
861         string const encodedFilename = d->filename.toFilesystemEncoding();
862
863         FileName backupName;
864         bool madeBackup = false;
865
866         // make a backup if the file already exists
867         if (lyxrc.make_backup && fileName().exists()) {
868                 backupName = FileName(absFileName() + '~');
869                 if (!lyxrc.backupdir_path.empty()) {
870                         string const mangledName =
871                                 subst(subst(backupName.absFilename(), '/', '!'), ':', '!');
872                         backupName = FileName(addName(lyxrc.backupdir_path,
873                                                       mangledName));
874                 }
875                 if (fileName().copyTo(backupName)) {
876                         madeBackup = true;
877                 } else {
878                         Alert::error(_("Backup failure"),
879                                      bformat(_("Cannot create backup file %1$s.\n"
880                                                "Please check whether the directory exists and is writeable."),
881                                              from_utf8(backupName.absFilename())));
882                         //LYXERR(Debug::DEBUG, "Fs error: " << fe.what());
883                 }
884         }
885
886         // ask if the disk file has been externally modified (use checksum method)
887         if (fileName().exists() && isExternallyModified(checksum_method)) {
888                 docstring const file = makeDisplayPath(absFileName(), 20);
889                 docstring text = bformat(_("Document %1$s has been externally modified. Are you sure "
890                                                              "you want to overwrite this file?"), file);
891                 int const ret = Alert::prompt(_("Overwrite modified file?"),
892                         text, 1, 1, _("&Overwrite"), _("&Cancel"));
893                 if (ret == 1)
894                         return false;
895         }
896
897         if (writeFile(d->filename)) {
898                 markClean();
899                 return true;
900         } else {
901                 // Saving failed, so backup is not backup
902                 if (madeBackup)
903                         backupName.moveTo(d->filename);
904                 return false;
905         }
906 }
907
908
909 bool Buffer::writeFile(FileName const & fname) const
910 {
911         if (d->read_only && fname == d->filename)
912                 return false;
913
914         bool retval = false;
915
916         docstring const str = bformat(_("Saving document %1$s..."),
917                 makeDisplayPath(fname.absFilename()));
918         message(str);
919
920         if (params().compressed) {
921                 gz::ogzstream ofs(fname.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
922                 retval = ofs && write(ofs);
923         } else {
924                 ofstream ofs(fname.toFilesystemEncoding().c_str(), ios::out|ios::trunc);
925                 retval = ofs && write(ofs);
926         }
927
928         if (!retval) {
929                 message(str + _(" could not write file!"));
930                 return false;
931         }
932
933         removeAutosaveFile(d->filename.absFilename());
934
935         saveCheckSum(d->filename);
936         message(str + _(" done."));
937
938         return true;
939 }
940
941
942 bool Buffer::write(ostream & ofs) const
943 {
944 #ifdef HAVE_LOCALE
945         // Use the standard "C" locale for file output.
946         ofs.imbue(locale::classic());
947 #endif
948
949         // The top of the file should not be written by params().
950
951         // write out a comment in the top of the file
952         ofs << "#LyX " << lyx_version
953             << " created this file. For more info see http://www.lyx.org/\n"
954             << "\\lyxformat " << LYX_FORMAT << "\n"
955             << "\\begin_document\n";
956
957         /// For each author, set 'used' to true if there is a change
958         /// by this author in the document; otherwise set it to 'false'.
959         AuthorList::Authors::const_iterator a_it = params().authors().begin();
960         AuthorList::Authors::const_iterator a_end = params().authors().end();
961         for (; a_it != a_end; ++a_it)
962                 a_it->second.setUsed(false);
963
964         ParIterator const end = const_cast<Buffer *>(this)->par_iterator_end();
965         ParIterator it = const_cast<Buffer *>(this)->par_iterator_begin();
966         for ( ; it != end; ++it)
967                 it->checkAuthors(params().authors());
968
969         // now write out the buffer parameters.
970         ofs << "\\begin_header\n";
971         params().writeFile(ofs);
972         ofs << "\\end_header\n";
973
974         // write the text
975         ofs << "\n\\begin_body\n";
976         text().write(*this, ofs);
977         ofs << "\n\\end_body\n";
978
979         // Write marker that shows file is complete
980         ofs << "\\end_document" << endl;
981
982         // Shouldn't really be needed....
983         //ofs.close();
984
985         // how to check if close went ok?
986         // Following is an attempt... (BE 20001011)
987
988         // good() returns false if any error occured, including some
989         //        formatting error.
990         // bad()  returns true if something bad happened in the buffer,
991         //        which should include file system full errors.
992
993         bool status = true;
994         if (!ofs) {
995                 status = false;
996                 lyxerr << "File was not closed properly." << endl;
997         }
998
999         return status;
1000 }
1001
1002
1003 bool Buffer::makeLaTeXFile(FileName const & fname,
1004                            string const & original_path,
1005                            OutputParams const & runparams,
1006                            bool output_preamble, bool output_body) const
1007 {
1008         string const encoding = runparams.encoding->iconvName();
1009         LYXERR(Debug::LATEX, "makeLaTeXFile encoding: " << encoding << "...");
1010
1011         ofdocstream ofs;
1012         try { ofs.reset(encoding); }
1013         catch (iconv_codecvt_facet_exception & e) {
1014                 lyxerr << "Caught iconv exception: " << e.what() << endl;
1015                 Alert::error(_("Iconv software exception Detected"), bformat(_("Please "
1016                         "verify that the support software for your encoding (%1$s) is "
1017                         "properly installed"), from_ascii(encoding)));
1018                 return false;
1019         }
1020         if (!openFileWrite(ofs, fname))
1021                 return false;
1022
1023         //TexStream ts(ofs.rdbuf(), &texrow());
1024         ErrorList & errorList = d->errorLists["Export"];
1025         errorList.clear();
1026         bool failed_export = false;
1027         try {
1028                 d->texrow.reset();
1029                 writeLaTeXSource(ofs, original_path,
1030                       runparams, output_preamble, output_body);
1031         }
1032         catch (EncodingException & e) {
1033                 odocstringstream ods;
1034                 ods.put(e.failed_char);
1035                 ostringstream oss;
1036                 oss << "0x" << hex << e.failed_char << dec;
1037                 docstring msg = bformat(_("Could not find LaTeX command for character '%1$s'"
1038                                           " (code point %2$s)"),
1039                                           ods.str(), from_utf8(oss.str()));
1040                 errorList.push_back(ErrorItem(msg, _("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                                 e.par_id, e.pos, e.pos + 1));
1044                 failed_export = true;
1045         }
1046         catch (iconv_codecvt_facet_exception & e) {
1047                 errorList.push_back(ErrorItem(_("iconv conversion failed"),
1048                         _(e.what()), -1, 0, 0));
1049                 failed_export = true;
1050         }
1051         catch (exception const & e) {
1052                 errorList.push_back(ErrorItem(_("conversion failed"),
1053                         _(e.what()), -1, 0, 0));
1054                 failed_export = true;
1055         }
1056         catch (...) {
1057                 lyxerr << "Caught some really weird exception..." << endl;
1058                 lyx_exit(1);
1059         }
1060
1061         ofs.close();
1062         if (ofs.fail()) {
1063                 failed_export = true;
1064                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1065         }
1066
1067         errors("Export");
1068         return !failed_export;
1069 }
1070
1071
1072 void Buffer::writeLaTeXSource(odocstream & os,
1073                            string const & original_path,
1074                            OutputParams const & runparams_in,
1075                            bool const output_preamble, bool const output_body) const
1076 {
1077         // The child documents, if any, shall be already loaded at this point.
1078
1079         OutputParams runparams = runparams_in;
1080
1081         // Classify the unicode characters appearing in math insets
1082         Encodings::initUnicodeMath(*this);
1083
1084         // validate the buffer.
1085         LYXERR(Debug::LATEX, "  Validating buffer...");
1086         LaTeXFeatures features(*this, params(), runparams);
1087         validate(features);
1088         LYXERR(Debug::LATEX, "  Buffer validation done.");
1089
1090         // The starting paragraph of the coming rows is the
1091         // first paragraph of the document. (Asger)
1092         if (output_preamble && runparams.nice) {
1093                 os << "%% LyX " << lyx_version << " created this file.  "
1094                         "For more info, see http://www.lyx.org/.\n"
1095                         "%% Do not edit unless you really know what "
1096                         "you are doing.\n";
1097                 d->texrow.newline();
1098                 d->texrow.newline();
1099         }
1100         LYXERR(Debug::INFO, "lyx document header finished");
1101
1102         // Don't move this behind the parent_buffer=0 code below,
1103         // because then the macros will not get the right "redefinition"
1104         // flag as they don't see the parent macros which are output before.
1105         updateMacros();
1106
1107         // fold macros if possible, still with parent buffer as the
1108         // macros will be put in the prefix anyway.
1109         updateMacroInstances();
1110
1111         // There are a few differences between nice LaTeX and usual files:
1112         // usual is \batchmode and has a
1113         // special input@path to allow the including of figures
1114         // with either \input or \includegraphics (what figinsets do).
1115         // input@path is set when the actual parameter
1116         // original_path is set. This is done for usual tex-file, but not
1117         // for nice-latex-file. (Matthias 250696)
1118         // Note that input@path is only needed for something the user does
1119         // in the preamble, included .tex files or ERT, files included by
1120         // LyX work without it.
1121         if (output_preamble) {
1122                 if (!runparams.nice) {
1123                         // code for usual, NOT nice-latex-file
1124                         os << "\\batchmode\n"; // changed
1125                         // from \nonstopmode
1126                         d->texrow.newline();
1127                 }
1128                 if (!original_path.empty()) {
1129                         // FIXME UNICODE
1130                         // We don't know the encoding of inputpath
1131                         docstring const inputpath = from_utf8(latex_path(original_path));
1132                         os << "\\makeatletter\n"
1133                            << "\\def\\input@path{{"
1134                            << inputpath << "/}}\n"
1135                            << "\\makeatother\n";
1136                         d->texrow.newline();
1137                         d->texrow.newline();
1138                         d->texrow.newline();
1139                 }
1140
1141                 // get parent macros (if this buffer has a parent) which will be
1142                 // written at the document begin further down.
1143                 MacroSet parentMacros;
1144                 listParentMacros(parentMacros, features);
1145
1146                 // Write the preamble
1147                 runparams.use_babel = params().writeLaTeX(os, features, d->texrow);
1148
1149                 runparams.use_japanese = features.isRequired("japanese");
1150
1151                 if (!output_body)
1152                         return;
1153
1154                 // make the body.
1155                 os << "\\begin{document}\n";
1156                 d->texrow.newline();
1157
1158                 // output the parent macros
1159                 MacroSet::iterator it = parentMacros.begin();
1160                 MacroSet::iterator end = parentMacros.end();
1161                 for (; it != end; ++it)
1162                         (*it)->write(os, true);
1163         } // output_preamble
1164
1165         d->texrow.start(paragraphs().begin()->id(), 0);
1166
1167         LYXERR(Debug::INFO, "preamble finished, now the body.");
1168
1169         // if we are doing a real file with body, even if this is the
1170         // child of some other buffer, let's cut the link here.
1171         // This happens for example if only a child document is printed.
1172         Buffer const * save_parent = 0;
1173         if (output_preamble) {
1174                 save_parent = d->parent();
1175                 d->setParent(0);
1176         }
1177
1178         // the real stuff
1179         latexParagraphs(*this, text(), os, d->texrow, runparams);
1180
1181         // Restore the parenthood if needed
1182         if (output_preamble)
1183                 d->setParent(save_parent);
1184
1185         // add this just in case after all the paragraphs
1186         os << endl;
1187         d->texrow.newline();
1188
1189         if (output_preamble) {
1190                 os << "\\end{document}\n";
1191                 d->texrow.newline();
1192                 LYXERR(Debug::LATEX, "makeLaTeXFile...done");
1193         } else {
1194                 LYXERR(Debug::LATEX, "LaTeXFile for inclusion made.");
1195         }
1196         runparams_in.encoding = runparams.encoding;
1197
1198         // Just to be sure. (Asger)
1199         d->texrow.newline();
1200
1201         LYXERR(Debug::INFO, "Finished making LaTeX file.");
1202         LYXERR(Debug::INFO, "Row count was " << d->texrow.rows() - 1 << '.');
1203 }
1204
1205
1206 bool Buffer::isLatex() const
1207 {
1208         return params().documentClass().outputType() == LATEX;
1209 }
1210
1211
1212 bool Buffer::isLiterate() const
1213 {
1214         return params().documentClass().outputType() == LITERATE;
1215 }
1216
1217
1218 bool Buffer::isDocBook() const
1219 {
1220         return params().documentClass().outputType() == DOCBOOK;
1221 }
1222
1223
1224 void Buffer::makeDocBookFile(FileName const & fname,
1225                               OutputParams const & runparams,
1226                               bool const body_only) const
1227 {
1228         LYXERR(Debug::LATEX, "makeDocBookFile...");
1229
1230         ofdocstream ofs;
1231         if (!openFileWrite(ofs, fname))
1232                 return;
1233
1234         writeDocBookSource(ofs, fname.absFilename(), runparams, body_only);
1235
1236         ofs.close();
1237         if (ofs.fail())
1238                 lyxerr << "File '" << fname << "' was not closed properly." << endl;
1239 }
1240
1241
1242 void Buffer::writeDocBookSource(odocstream & os, string const & fname,
1243                              OutputParams const & runparams,
1244                              bool const only_body) const
1245 {
1246         LaTeXFeatures features(*this, params(), runparams);
1247         validate(features);
1248
1249         d->texrow.reset();
1250
1251         DocumentClass const & tclass = params().documentClass();
1252         string const top_element = tclass.latexname();
1253
1254         if (!only_body) {
1255                 if (runparams.flavor == OutputParams::XML)
1256                         os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1257
1258                 // FIXME UNICODE
1259                 os << "<!DOCTYPE " << from_ascii(top_element) << ' ';
1260
1261                 // FIXME UNICODE
1262                 if (! tclass.class_header().empty())
1263                         os << from_ascii(tclass.class_header());
1264                 else if (runparams.flavor == OutputParams::XML)
1265                         os << "PUBLIC \"-//OASIS//DTD DocBook XML//EN\" "
1266                             << "\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\"";
1267                 else
1268                         os << " PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\"";
1269
1270                 docstring preamble = from_utf8(params().preamble);
1271                 if (runparams.flavor != OutputParams::XML ) {
1272                         preamble += "<!ENTITY % output.print.png \"IGNORE\">\n";
1273                         preamble += "<!ENTITY % output.print.pdf \"IGNORE\">\n";
1274                         preamble += "<!ENTITY % output.print.eps \"IGNORE\">\n";
1275                         preamble += "<!ENTITY % output.print.bmp \"IGNORE\">\n";
1276                 }
1277
1278                 string const name = runparams.nice
1279                         ? changeExtension(absFileName(), ".sgml") : fname;
1280                 preamble += features.getIncludedFiles(name);
1281                 preamble += features.getLyXSGMLEntities();
1282
1283                 if (!preamble.empty()) {
1284                         os << "\n [ " << preamble << " ]";
1285                 }
1286                 os << ">\n\n";
1287         }
1288
1289         string top = top_element;
1290         top += " lang=\"";
1291         if (runparams.flavor == OutputParams::XML)
1292                 top += params().language->code();
1293         else
1294                 top += params().language->code().substr(0, 2);
1295         top += '"';
1296
1297         if (!params().options.empty()) {
1298                 top += ' ';
1299                 top += params().options;
1300         }
1301
1302         os << "<!-- " << ((runparams.flavor == OutputParams::XML)? "XML" : "SGML")
1303             << " file was created by LyX " << lyx_version
1304             << "\n  See http://www.lyx.org/ for more information -->\n";
1305
1306         params().documentClass().counters().reset();
1307
1308         updateMacros();
1309
1310         sgml::openTag(os, top);
1311         os << '\n';
1312         docbookParagraphs(paragraphs(), *this, os, runparams);
1313         sgml::closeTag(os, top_element);
1314 }
1315
1316
1317 // chktex should be run with these flags disabled: 3, 22, 25, 30, 38(?)
1318 // Other flags: -wall -v0 -x
1319 int Buffer::runChktex()
1320 {
1321         setBusy(true);
1322
1323         // get LaTeX-Filename
1324         FileName const path(temppath());
1325         string const name = addName(path.absFilename(), latexName());
1326         string const org_path = filePath();
1327
1328         PathChanger p(path); // path to LaTeX file
1329         message(_("Running chktex..."));
1330
1331         // Generate the LaTeX file if neccessary
1332         OutputParams runparams(&params().encoding());
1333         runparams.flavor = OutputParams::LATEX;
1334         runparams.nice = false;
1335         makeLaTeXFile(FileName(name), org_path, runparams);
1336
1337         TeXErrors terr;
1338         Chktex chktex(lyxrc.chktex_command, onlyFilename(name), filePath());
1339         int const res = chktex.run(terr); // run chktex
1340
1341         if (res == -1) {
1342                 Alert::error(_("chktex failure"),
1343                              _("Could not run chktex successfully."));
1344         } else if (res > 0) {
1345                 ErrorList & errlist = d->errorLists["ChkTeX"];
1346                 errlist.clear();
1347                 bufferErrors(terr, errlist);
1348         }
1349
1350         setBusy(false);
1351
1352         errors("ChkTeX");
1353
1354         return res;
1355 }
1356
1357
1358 void Buffer::validate(LaTeXFeatures & features) const
1359 {
1360         params().validate(features);
1361
1362         updateMacros();
1363
1364         for_each(paragraphs().begin(), paragraphs().end(),
1365                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1366
1367         if (lyxerr.debugging(Debug::LATEX)) {
1368                 features.showStruct();
1369         }
1370 }
1371
1372
1373 void Buffer::getLabelList(vector<docstring> & list) const
1374 {
1375         // If this is a child document, use the parent's list instead.
1376         Buffer const * const pbuf = d->parent();
1377         if (pbuf) {
1378                 pbuf->getLabelList(list);
1379                 return;
1380         }
1381
1382         list.clear();
1383         Toc & toc = d->toc_backend.toc("label");
1384         TocIterator toc_it = toc.begin();
1385         TocIterator end = toc.end();
1386         for (; toc_it != end; ++toc_it) {
1387                 if (toc_it->depth() == 0)
1388                         list.push_back(toc_it->str());
1389         }
1390 }
1391
1392
1393 void Buffer::updateBibfilesCache(UpdateScope scope) const
1394 {
1395         // If this is a child document, use the parent's cache instead.
1396         Buffer const * const pbuf = d->parent();
1397         if (pbuf && scope != UpdateChildOnly) {
1398                 pbuf->updateBibfilesCache();
1399                 return;
1400         }
1401
1402         d->bibfilesCache_.clear();
1403         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
1404                 if (it->lyxCode() == BIBTEX_CODE) {
1405                         InsetBibtex const & inset =
1406                                 static_cast<InsetBibtex const &>(*it);
1407                         support::FileNameList const bibfiles = inset.getBibFiles();
1408                         d->bibfilesCache_.insert(d->bibfilesCache_.end(),
1409                                 bibfiles.begin(),
1410                                 bibfiles.end());
1411                 } else if (it->lyxCode() == INCLUDE_CODE) {
1412                         InsetInclude & inset =
1413                                 static_cast<InsetInclude &>(*it);
1414                         inset.updateBibfilesCache();
1415                         support::FileNameList const & bibfiles =
1416                                         inset.getBibfilesCache();
1417                         d->bibfilesCache_.insert(d->bibfilesCache_.end(),
1418                                 bibfiles.begin(),
1419                                 bibfiles.end());
1420                 }
1421         }
1422         // the bibinfo cache is now invalid
1423         d->bibinfoCacheValid_ = false;
1424 }
1425
1426
1427 void Buffer::invalidateBibinfoCache()
1428 {
1429         d->bibinfoCacheValid_ = false;
1430 }
1431
1432
1433 support::FileNameList const & Buffer::getBibfilesCache(UpdateScope scope) const
1434 {
1435         // If this is a child document, use the parent's cache instead.
1436         Buffer const * const pbuf = d->parent();
1437         if (pbuf && scope != UpdateChildOnly)
1438                 return pbuf->getBibfilesCache();
1439
1440         // We update the cache when first used instead of at loading time.
1441         if (d->bibfilesCache_.empty())
1442                 const_cast<Buffer *>(this)->updateBibfilesCache(scope);
1443
1444         return d->bibfilesCache_;
1445 }
1446
1447
1448 BiblioInfo const & Buffer::masterBibInfo() const
1449 {
1450         // if this is a child document and the parent is already loaded
1451         // use the parent's list instead  [ale990412]
1452         Buffer const * const tmp = masterBuffer();
1453         LASSERT(tmp, /**/);
1454         if (tmp != this)
1455                 return tmp->masterBibInfo();
1456         return localBibInfo();
1457 }
1458
1459
1460 BiblioInfo const & Buffer::localBibInfo() const
1461 {
1462         if (d->bibinfoCacheValid_) {
1463                 support::FileNameList const & bibfilesCache = getBibfilesCache();
1464                 // compare the cached timestamps with the actual ones.
1465                 support::FileNameList::const_iterator ei = bibfilesCache.begin();
1466                 support::FileNameList::const_iterator en = bibfilesCache.end();
1467                 for (; ei != en; ++ ei) {
1468                         time_t lastw = ei->lastModified();
1469                         if (lastw != d->bibfileStatus_[*ei]) {
1470                                 d->bibinfoCacheValid_ = false;
1471                                 d->bibfileStatus_[*ei] = lastw;
1472                                 break;
1473                         }
1474                 }
1475         }
1476
1477         if (!d->bibinfoCacheValid_) {
1478                 d->bibinfo_.clear();
1479                 for (InsetIterator it = inset_iterator_begin(inset()); it; ++it)
1480                         it->fillWithBibKeys(d->bibinfo_, it);
1481                 d->bibinfoCacheValid_ = true;
1482         }
1483         return d->bibinfo_;
1484 }
1485
1486
1487 bool Buffer::isDepClean(string const & name) const
1488 {
1489         DepClean::const_iterator const it = d->dep_clean.find(name);
1490         if (it == d->dep_clean.end())
1491                 return true;
1492         return it->second;
1493 }
1494
1495
1496 void Buffer::markDepClean(string const & name)
1497 {
1498         d->dep_clean[name] = true;
1499 }
1500
1501
1502 bool Buffer::dispatch(string const & command, bool * result)
1503 {
1504         return dispatch(lyxaction.lookupFunc(command), result);
1505 }
1506
1507
1508 bool Buffer::dispatch(FuncRequest const & func, bool * result)
1509 {
1510         bool dispatched = true;
1511
1512         switch (func.action) {
1513                 case LFUN_BUFFER_EXPORT: {
1514                         bool const tmp = doExport(to_utf8(func.argument()), false);
1515                         if (result)
1516                                 *result = tmp;
1517                         break;
1518                 }
1519
1520                 case LFUN_BRANCH_ACTIVATE:
1521                 case LFUN_BRANCH_DEACTIVATE: {
1522                         BranchList & branchList = params().branchlist();
1523                         docstring const branchName = func.argument();
1524                         // the case without a branch name is handled elsewhere
1525                         if (branchName.empty()) {
1526                                 dispatched = false;
1527                                 break;
1528                         }
1529                         Branch * branch = branchList.find(branchName);
1530                         if (!branch)
1531                                 LYXERR0("Branch " << branchName << " does not exist.");
1532                         else
1533                                 branch->setSelected(func.action == LFUN_BRANCH_ACTIVATE);
1534                         if (result)
1535                                 *result = true;
1536                 }
1537
1538                 default:
1539                         dispatched = false;
1540         }
1541         return dispatched;
1542 }
1543
1544
1545 void Buffer::changeLanguage(Language const * from, Language const * to)
1546 {
1547         LASSERT(from, /**/);
1548         LASSERT(to, /**/);
1549
1550         for_each(par_iterator_begin(),
1551                  par_iterator_end(),
1552                  bind(&Paragraph::changeLanguage, _1, params(), from, to));
1553 }
1554
1555
1556 bool Buffer::isMultiLingual() const
1557 {
1558         ParConstIterator end = par_iterator_end();
1559         for (ParConstIterator it = par_iterator_begin(); it != end; ++it)
1560                 if (it->isMultiLingual(params()))
1561                         return true;
1562
1563         return false;
1564 }
1565
1566
1567 DocIterator Buffer::getParFromID(int const id) const
1568 {
1569         Buffer * buf = const_cast<Buffer *>(this);
1570         if (id < 0) {
1571                 // John says this is called with id == -1 from undo
1572                 lyxerr << "getParFromID(), id: " << id << endl;
1573                 return doc_iterator_end(buf);
1574         }
1575
1576         for (DocIterator it = doc_iterator_begin(buf); !it.atEnd(); it.forwardPar())
1577                 if (it.paragraph().id() == id)
1578                         return it;
1579
1580         return doc_iterator_end(buf);
1581 }
1582
1583
1584 bool Buffer::hasParWithID(int const id) const
1585 {
1586         return !getParFromID(id).atEnd();
1587 }
1588
1589
1590 ParIterator Buffer::par_iterator_begin()
1591 {
1592         return ParIterator(doc_iterator_begin(this));
1593 }
1594
1595
1596 ParIterator Buffer::par_iterator_end()
1597 {
1598         return ParIterator(doc_iterator_end(this));
1599 }
1600
1601
1602 ParConstIterator Buffer::par_iterator_begin() const
1603 {
1604         return ParConstIterator(doc_iterator_begin(this));
1605 }
1606
1607
1608 ParConstIterator Buffer::par_iterator_end() const
1609 {
1610         return ParConstIterator(doc_iterator_end(this));
1611 }
1612
1613
1614 Language const * Buffer::language() const
1615 {
1616         return params().language;
1617 }
1618
1619
1620 docstring const Buffer::B_(string const & l10n) const
1621 {
1622         return params().B_(l10n);
1623 }
1624
1625
1626 bool Buffer::isClean() const
1627 {
1628         return d->lyx_clean;
1629 }
1630
1631
1632 bool Buffer::isBakClean() const
1633 {
1634         return d->bak_clean;
1635 }
1636
1637
1638 bool Buffer::isExternallyModified(CheckMethod method) const
1639 {
1640         LASSERT(d->filename.exists(), /**/);
1641         // if method == timestamp, check timestamp before checksum
1642         return (method == checksum_method
1643                 || d->timestamp_ != d->filename.lastModified())
1644                 && d->checksum_ != d->filename.checksum();
1645 }
1646
1647
1648 void Buffer::saveCheckSum(FileName const & file) const
1649 {
1650         if (file.exists()) {
1651                 d->timestamp_ = file.lastModified();
1652                 d->checksum_ = file.checksum();
1653         } else {
1654                 // in the case of save to a new file.
1655                 d->timestamp_ = 0;
1656                 d->checksum_ = 0;
1657         }
1658 }
1659
1660
1661 void Buffer::markClean() const
1662 {
1663         if (!d->lyx_clean) {
1664                 d->lyx_clean = true;
1665                 updateTitles();
1666         }
1667         // if the .lyx file has been saved, we don't need an
1668         // autosave
1669         d->bak_clean = true;
1670 }
1671
1672
1673 void Buffer::markBakClean() const
1674 {
1675         d->bak_clean = true;
1676 }
1677
1678
1679 void Buffer::setUnnamed(bool flag)
1680 {
1681         d->unnamed = flag;
1682 }
1683
1684
1685 bool Buffer::isUnnamed() const
1686 {
1687         return d->unnamed;
1688 }
1689
1690
1691 // FIXME: this function should be moved to buffer_pimpl.C
1692 void Buffer::markDirty()
1693 {
1694         if (d->lyx_clean) {
1695                 d->lyx_clean = false;
1696                 updateTitles();
1697         }
1698         d->bak_clean = false;
1699
1700         DepClean::iterator it = d->dep_clean.begin();
1701         DepClean::const_iterator const end = d->dep_clean.end();
1702
1703         for (; it != end; ++it)
1704                 it->second = false;
1705 }
1706
1707
1708 FileName Buffer::fileName() const
1709 {
1710         return d->filename;
1711 }
1712
1713
1714 string Buffer::absFileName() const
1715 {
1716         return d->filename.absFilename();
1717 }
1718
1719
1720 string Buffer::filePath() const
1721 {
1722         return d->filename.onlyPath().absFilename() + "/";
1723 }
1724
1725
1726 bool Buffer::isReadonly() const
1727 {
1728         return d->read_only;
1729 }
1730
1731
1732 void Buffer::setParent(Buffer const * buffer)
1733 {
1734         // Avoids recursive include.
1735         d->setParent(buffer == this ? 0 : buffer);
1736         updateMacros();
1737 }
1738
1739
1740 Buffer const * Buffer::parent() const
1741 {
1742         return d->parent();
1743 }
1744
1745
1746 void Buffer::collectRelatives(BufferSet & bufs) const
1747 {
1748         bufs.insert(this);
1749         if (parent())
1750                 parent()->collectRelatives(bufs);
1751
1752         // loop over children
1753         Impl::BufferPositionMap::iterator it = d->children_positions.begin();
1754         Impl::BufferPositionMap::iterator end = d->children_positions.end();
1755         for (; it != end; ++it)
1756                 bufs.insert(const_cast<Buffer *>(it->first));
1757 }
1758
1759
1760 std::vector<Buffer const *> Buffer::allRelatives() const
1761 {
1762         BufferSet bufs;
1763         collectRelatives(bufs);
1764         BufferSet::iterator it = bufs.begin();
1765         std::vector<Buffer const *> ret;
1766         for (; it != bufs.end(); ++it)
1767                 ret.push_back(*it);
1768         return ret;
1769 }
1770
1771
1772 Buffer const * Buffer::masterBuffer() const
1773 {
1774         Buffer const * const pbuf = d->parent();
1775         if (!pbuf)
1776                 return this;
1777
1778         return pbuf->masterBuffer();
1779 }
1780
1781
1782 bool Buffer::isChild(Buffer * child) const
1783 {
1784         return d->children_positions.find(child) != d->children_positions.end();
1785 }
1786
1787
1788 DocIterator Buffer::firstChildPosition(Buffer const * child)
1789 {
1790         Impl::BufferPositionMap::iterator it;
1791         it = d->children_positions.find(child);
1792         if (it == d->children_positions.end())
1793                 return DocIterator(this);
1794         return it->second;
1795 }
1796
1797
1798 std::vector<Buffer *> Buffer::getChildren() const
1799 {
1800         std::vector<Buffer *> clist;
1801         // loop over children
1802         Impl::BufferPositionMap::iterator it = d->children_positions.begin();
1803         Impl::BufferPositionMap::iterator end = d->children_positions.end();
1804         for (; it != end; ++it) {
1805                 Buffer * child = const_cast<Buffer *>(it->first);
1806                 clist.push_back(child);
1807                 // there might be grandchildren
1808                 std::vector<Buffer *> glist = child->getChildren();
1809                 for (vector<Buffer *>::const_iterator git = glist.begin();
1810                      git != glist.end(); ++git)
1811                         clist.push_back(*git);
1812         }
1813         return clist;
1814 }
1815
1816
1817 template<typename M>
1818 typename M::iterator greatest_below(M & m, typename M::key_type const & x)
1819 {
1820         if (m.empty())
1821                 return m.end();
1822
1823         typename M::iterator it = m.lower_bound(x);
1824         if (it == m.begin())
1825                 return m.end();
1826
1827         it--;
1828         return it;
1829 }
1830
1831
1832 MacroData const * Buffer::getBufferMacro(docstring const & name,
1833                                          DocIterator const & pos) const
1834 {
1835         LYXERR(Debug::MACROS, "Searching for " << to_ascii(name) << " at " << pos);
1836
1837         // if paragraphs have no macro context set, pos will be empty
1838         if (pos.empty())
1839                 return 0;
1840
1841         // we haven't found anything yet
1842         DocIterator bestPos = par_iterator_begin();
1843         MacroData const * bestData = 0;
1844
1845         // find macro definitions for name
1846         Impl::NamePositionScopeMacroMap::iterator nameIt
1847                 = d->macros.find(name);
1848         if (nameIt != d->macros.end()) {
1849                 // find last definition in front of pos or at pos itself
1850                 Impl::PositionScopeMacroMap::const_iterator it
1851                         = greatest_below(nameIt->second, pos);
1852                 if (it != nameIt->second.end()) {
1853                         while (true) {
1854                                 // scope ends behind pos?
1855                                 if (pos < it->second.first) {
1856                                         // Looks good, remember this. If there
1857                                         // is no external macro behind this,
1858                                         // we found the right one already.
1859                                         bestPos = it->first;
1860                                         bestData = &it->second.second;
1861                                         break;
1862                                 }
1863
1864                                 // try previous macro if there is one
1865                                 if (it == nameIt->second.begin())
1866                                         break;
1867                                 it--;
1868                         }
1869                 }
1870         }
1871
1872         // find macros in included files
1873         Impl::PositionScopeBufferMap::const_iterator it
1874                 = greatest_below(d->position_to_children, pos);
1875         if (it == d->position_to_children.end())
1876                 // no children before
1877                 return bestData;
1878
1879         while (true) {
1880                 // do we know something better (i.e. later) already?
1881                 if (it->first < bestPos )
1882                         break;
1883
1884                 // scope ends behind pos?
1885                 if (pos < it->second.first) {
1886                         // look for macro in external file
1887                         d->macro_lock = true;
1888                         MacroData const * data
1889                         = it->second.second->getMacro(name, false);
1890                         d->macro_lock = false;
1891                         if (data) {
1892                                 bestPos = it->first;
1893                                 bestData = data;
1894                                 break;
1895                         }
1896                 }
1897
1898                 // try previous file if there is one
1899                 if (it == d->position_to_children.begin())
1900                         break;
1901                 --it;
1902         }
1903
1904         // return the best macro we have found
1905         return bestData;
1906 }
1907
1908
1909 MacroData const * Buffer::getMacro(docstring const & name,
1910         DocIterator const & pos, bool global) const
1911 {
1912         if (d->macro_lock)
1913                 return 0;
1914
1915         // query buffer macros
1916         MacroData const * data = getBufferMacro(name, pos);
1917         if (data != 0)
1918                 return data;
1919
1920         // If there is a master buffer, query that
1921         Buffer const * const pbuf = d->parent();
1922         if (pbuf) {
1923                 d->macro_lock = true;
1924                 MacroData const * macro = pbuf->getMacro(
1925                         name, *this, false);
1926                 d->macro_lock = false;
1927                 if (macro)
1928                         return macro;
1929         }
1930
1931         if (global) {
1932                 data = MacroTable::globalMacros().get(name);
1933                 if (data != 0)
1934                         return data;
1935         }
1936
1937         return 0;
1938 }
1939
1940
1941 MacroData const * Buffer::getMacro(docstring const & name, bool global) const
1942 {
1943         // set scope end behind the last paragraph
1944         DocIterator scope = par_iterator_begin();
1945         scope.pit() = scope.lastpit() + 1;
1946
1947         return getMacro(name, scope, global);
1948 }
1949
1950
1951 MacroData const * Buffer::getMacro(docstring const & name,
1952         Buffer const & child, bool global) const
1953 {
1954         // look where the child buffer is included first
1955         Impl::BufferPositionMap::iterator it = d->children_positions.find(&child);
1956         if (it == d->children_positions.end())
1957                 return 0;
1958
1959         // check for macros at the inclusion position
1960         return getMacro(name, it->second, global);
1961 }
1962
1963
1964 void Buffer::updateMacros(DocIterator & it, DocIterator & scope) const
1965 {
1966         pit_type lastpit = it.lastpit();
1967
1968         // look for macros in each paragraph
1969         while (it.pit() <= lastpit) {
1970                 Paragraph & par = it.paragraph();
1971
1972                 // iterate over the insets of the current paragraph
1973                 InsetList const & insets = par.insetList();
1974                 InsetList::const_iterator iit = insets.begin();
1975                 InsetList::const_iterator end = insets.end();
1976                 for (; iit != end; ++iit) {
1977                         it.pos() = iit->pos;
1978
1979                         // is it a nested text inset?
1980                         if (iit->inset->asInsetText()) {
1981                                 // Inset needs its own scope?
1982                                 InsetText const * itext = iit->inset->asInsetText();
1983                                 bool newScope = itext->isMacroScope();
1984
1985                                 // scope which ends just behind the inset
1986                                 DocIterator insetScope = it;
1987                                 ++insetScope.pos();
1988
1989                                 // collect macros in inset
1990                                 it.push_back(CursorSlice(*iit->inset));
1991                                 updateMacros(it, newScope ? insetScope : scope);
1992                                 it.pop_back();
1993                                 continue;
1994                         }
1995
1996                         // is it an external file?
1997                         if (iit->inset->lyxCode() == INCLUDE_CODE) {
1998                                 // get buffer of external file
1999                                 InsetInclude const & inset =
2000                                         static_cast<InsetInclude const &>(*iit->inset);
2001                                 d->macro_lock = true;
2002                                 Buffer * child = inset.getChildBuffer();
2003                                 d->macro_lock = false;
2004                                 if (!child)
2005                                         continue;
2006
2007                                 // register its position, but only when it is
2008                                 // included first in the buffer
2009                                 if (d->children_positions.find(child) ==
2010                                         d->children_positions.end())
2011                                                 d->children_positions[child] = it;
2012
2013                                 // register child with its scope
2014                                 d->position_to_children[it] = Impl::ScopeBuffer(scope, child);
2015                                 continue;
2016                         }
2017
2018                         if (iit->inset->lyxCode() != MATHMACRO_CODE)
2019                                 continue;
2020
2021                         // get macro data
2022                         MathMacroTemplate & macroTemplate =
2023                                 static_cast<MathMacroTemplate &>(*iit->inset);
2024                         MacroContext mc(*this, it);
2025                         macroTemplate.updateToContext(mc);
2026
2027                         // valid?
2028                         bool valid = macroTemplate.validMacro();
2029                         // FIXME: Should be fixNameAndCheckIfValid() in fact,
2030                         // then the BufferView's cursor will be invalid in
2031                         // some cases which leads to crashes.
2032                         if (!valid)
2033                                 continue;
2034
2035                         // register macro
2036                         d->macros[macroTemplate.name()][it] =
2037                                 Impl::ScopeMacro(scope, MacroData(*this, it));
2038                 }
2039
2040                 // next paragraph
2041                 it.pit()++;
2042                 it.pos() = 0;
2043         }
2044 }
2045
2046
2047 void Buffer::updateMacros() const
2048 {
2049         if (d->macro_lock)
2050                 return;
2051
2052         LYXERR(Debug::MACROS, "updateMacro of " << d->filename.onlyFileName());
2053
2054         // start with empty table
2055         d->macros.clear();
2056         d->children_positions.clear();
2057         d->position_to_children.clear();
2058
2059         // Iterate over buffer, starting with first paragraph
2060         // The scope must be bigger than any lookup DocIterator
2061         // later. For the global lookup, lastpit+1 is used, hence
2062         // we use lastpit+2 here.
2063         DocIterator it = par_iterator_begin();
2064         DocIterator outerScope = it;
2065         outerScope.pit() = outerScope.lastpit() + 2;
2066         updateMacros(it, outerScope);
2067 }
2068
2069
2070 void Buffer::updateMacroInstances() const
2071 {
2072         LYXERR(Debug::MACROS, "updateMacroInstances for "
2073                 << d->filename.onlyFileName());
2074         DocIterator it = doc_iterator_begin(this);
2075         DocIterator end = doc_iterator_end(this);
2076         for (; it != end; it.forwardPos()) {
2077                 // look for MathData cells in InsetMathNest insets
2078                 Inset * inset = it.nextInset();
2079                 if (!inset)
2080                         continue;
2081
2082                 InsetMath * minset = inset->asInsetMath();
2083                 if (!minset)
2084                         continue;
2085
2086                 // update macro in all cells of the InsetMathNest
2087                 DocIterator::idx_type n = minset->nargs();
2088                 MacroContext mc = MacroContext(*this, it);
2089                 for (DocIterator::idx_type i = 0; i < n; ++i) {
2090                         MathData & data = minset->cell(i);
2091                         data.updateMacros(0, mc);
2092                 }
2093         }
2094 }
2095
2096
2097 void Buffer::listMacroNames(MacroNameSet & macros) const
2098 {
2099         if (d->macro_lock)
2100                 return;
2101
2102         d->macro_lock = true;
2103
2104         // loop over macro names
2105         Impl::NamePositionScopeMacroMap::iterator nameIt = d->macros.begin();
2106         Impl::NamePositionScopeMacroMap::iterator nameEnd = d->macros.end();
2107         for (; nameIt != nameEnd; ++nameIt)
2108                 macros.insert(nameIt->first);
2109
2110         // loop over children
2111         Impl::BufferPositionMap::iterator it = d->children_positions.begin();
2112         Impl::BufferPositionMap::iterator end = d->children_positions.end();
2113         for (; it != end; ++it)
2114                 it->first->listMacroNames(macros);
2115
2116         // call parent
2117         Buffer const * const pbuf = d->parent();
2118         if (pbuf)
2119                 pbuf->listMacroNames(macros);
2120
2121         d->macro_lock = false;
2122 }
2123
2124
2125 void Buffer::listParentMacros(MacroSet & macros, LaTeXFeatures & features) const
2126 {
2127         Buffer const * const pbuf = d->parent();
2128         if (!pbuf)
2129                 return;
2130
2131         MacroNameSet names;
2132         pbuf->listMacroNames(names);
2133
2134         // resolve macros
2135         MacroNameSet::iterator it = names.begin();
2136         MacroNameSet::iterator end = names.end();
2137         for (; it != end; ++it) {
2138                 // defined?
2139                 MacroData const * data =
2140                 pbuf->getMacro(*it, *this, false);
2141                 if (data) {
2142                         macros.insert(data);
2143
2144                         // we cannot access the original MathMacroTemplate anymore
2145                         // here to calls validate method. So we do its work here manually.
2146                         // FIXME: somehow make the template accessible here.
2147                         if (data->optionals() > 0)
2148                                 features.require("xargs");
2149                 }
2150         }
2151 }
2152
2153
2154 Buffer::References & Buffer::references(docstring const & label)
2155 {
2156         if (d->parent())
2157                 return const_cast<Buffer *>(masterBuffer())->references(label);
2158
2159         RefCache::iterator it = d->ref_cache_.find(label);
2160         if (it != d->ref_cache_.end())
2161                 return it->second.second;
2162
2163         static InsetLabel const * dummy_il = 0;
2164         static References const dummy_refs;
2165         it = d->ref_cache_.insert(
2166                 make_pair(label, make_pair(dummy_il, dummy_refs))).first;
2167         return it->second.second;
2168 }
2169
2170
2171 Buffer::References const & Buffer::references(docstring const & label) const
2172 {
2173         return const_cast<Buffer *>(this)->references(label);
2174 }
2175
2176
2177 void Buffer::setInsetLabel(docstring const & label, InsetLabel const * il)
2178 {
2179         masterBuffer()->d->ref_cache_[label].first = il;
2180 }
2181
2182
2183 InsetLabel const * Buffer::insetLabel(docstring const & label) const
2184 {
2185         return masterBuffer()->d->ref_cache_[label].first;
2186 }
2187
2188
2189 void Buffer::clearReferenceCache() const
2190 {
2191         if (!d->parent())
2192                 d->ref_cache_.clear();
2193 }
2194
2195
2196 void Buffer::changeRefsIfUnique(docstring const & from, docstring const & to,
2197         InsetCode code)
2198 {
2199         //FIXME: This does not work for child documents yet.
2200         LASSERT(code == CITE_CODE, /**/);
2201         // Check if the label 'from' appears more than once
2202         vector<docstring> labels;
2203         string paramName;
2204         BiblioInfo const & keys = masterBibInfo();
2205         BiblioInfo::const_iterator bit  = keys.begin();
2206         BiblioInfo::const_iterator bend = keys.end();
2207
2208         for (; bit != bend; ++bit)
2209                 // FIXME UNICODE
2210                 labels.push_back(bit->first);
2211         paramName = "key";
2212
2213         if (count(labels.begin(), labels.end(), from) > 1)
2214                 return;
2215
2216         for (InsetIterator it = inset_iterator_begin(inset()); it; ++it) {
2217                 if (it->lyxCode() == code) {
2218                         InsetCommand & inset = static_cast<InsetCommand &>(*it);
2219                         docstring const oldValue = inset.getParam(paramName);
2220                         if (oldValue == from)
2221                                 inset.setParam(paramName, to);
2222                 }
2223         }
2224 }
2225
2226
2227 void Buffer::getSourceCode(odocstream & os, pit_type par_begin,
2228         pit_type par_end, bool full_source) const
2229 {
2230         OutputParams runparams(&params().encoding());
2231         runparams.nice = true;
2232         runparams.flavor = OutputParams::LATEX;
2233         runparams.linelen = lyxrc.plaintext_linelen;
2234         // No side effect of file copying and image conversion
2235         runparams.dryrun = true;
2236
2237         d->texrow.reset();
2238         if (full_source) {
2239                 os << "% " << _("Preview source code") << "\n\n";
2240                 d->texrow.newline();
2241                 d->texrow.newline();
2242                 if (isDocBook())
2243                         writeDocBookSource(os, absFileName(), runparams, false);
2244                 else
2245                         // latex or literate
2246                         writeLaTeXSource(os, string(), runparams, true, true);
2247         } else {
2248                 runparams.par_begin = par_begin;
2249                 runparams.par_end = par_end;
2250                 if (par_begin + 1 == par_end) {
2251                         os << "% "
2252                            << bformat(_("Preview source code for paragraph %1$d"), par_begin)
2253                            << "\n\n";
2254                 } else {
2255                         os << "% "
2256                            << bformat(_("Preview source code from paragraph %1$s to %2$s"),
2257                                         convert<docstring>(par_begin),
2258                                         convert<docstring>(par_end - 1))
2259                            << "\n\n";
2260                 }
2261                 d->texrow.newline();
2262                 d->texrow.newline();
2263                 // output paragraphs
2264                 if (isDocBook())
2265                         docbookParagraphs(paragraphs(), *this, os, runparams);
2266                 else 
2267                         // latex or literate
2268                         latexParagraphs(*this, text(), os, d->texrow, runparams);
2269         }
2270 }
2271
2272
2273 ErrorList & Buffer::errorList(string const & type) const
2274 {
2275         static ErrorList emptyErrorList;
2276         map<string, ErrorList>::iterator I = d->errorLists.find(type);
2277         if (I == d->errorLists.end())
2278                 return emptyErrorList;
2279
2280         return I->second;
2281 }
2282
2283
2284 void Buffer::updateTocItem(std::string const & type,
2285         DocIterator const & dit) const
2286 {
2287         if (gui_)
2288                 gui_->updateTocItem(type, dit);
2289 }
2290
2291
2292 void Buffer::structureChanged() const
2293 {
2294         if (gui_)
2295                 gui_->structureChanged();
2296 }
2297
2298
2299 void Buffer::errors(string const & err) const
2300 {
2301         if (gui_)
2302                 gui_->errors(err);
2303 }
2304
2305
2306 void Buffer::message(docstring const & msg) const
2307 {
2308         if (gui_)
2309                 gui_->message(msg);
2310 }
2311
2312
2313 void Buffer::setBusy(bool on) const
2314 {
2315         if (gui_)
2316                 gui_->setBusy(on);
2317 }
2318
2319
2320 void Buffer::setReadOnly(bool on) const
2321 {
2322         if (d->wa_)
2323                 d->wa_->setReadOnly(on);
2324 }
2325
2326
2327 void Buffer::updateTitles() const
2328 {
2329         if (d->wa_)
2330                 d->wa_->updateTitles();
2331 }
2332
2333
2334 void Buffer::resetAutosaveTimers() const
2335 {
2336         if (gui_)
2337                 gui_->resetAutosaveTimers();
2338 }
2339
2340
2341 bool Buffer::hasGuiDelegate() const
2342 {
2343         return gui_;
2344 }
2345
2346
2347 void Buffer::setGuiDelegate(frontend::GuiBufferDelegate * gui)
2348 {
2349         gui_ = gui;
2350 }
2351
2352
2353
2354 namespace {
2355
2356 class AutoSaveBuffer : public ForkedProcess {
2357 public:
2358         ///
2359         AutoSaveBuffer(Buffer const & buffer, FileName const & fname)
2360                 : buffer_(buffer), fname_(fname) {}
2361         ///
2362         virtual boost::shared_ptr<ForkedProcess> clone() const
2363         {
2364                 return boost::shared_ptr<ForkedProcess>(new AutoSaveBuffer(*this));
2365         }
2366         ///
2367         int start()
2368         {
2369                 command_ = to_utf8(bformat(_("Auto-saving %1$s"),
2370                                                  from_utf8(fname_.absFilename())));
2371                 return run(DontWait);
2372         }
2373 private:
2374         ///
2375         virtual int generateChild();
2376         ///
2377         Buffer const & buffer_;
2378         FileName fname_;
2379 };
2380
2381
2382 int AutoSaveBuffer::generateChild()
2383 {
2384         // tmp_ret will be located (usually) in /tmp
2385         // will that be a problem?
2386         // Note that this calls ForkedCalls::fork(), so it's
2387         // ok cross-platform.
2388         pid_t const pid = fork();
2389         // If you want to debug the autosave
2390         // you should set pid to -1, and comment out the fork.
2391         if (pid != 0 && pid != -1)
2392                 return pid;
2393
2394         // pid = -1 signifies that lyx was unable
2395         // to fork. But we will do the save
2396         // anyway.
2397         bool failed = false;
2398         FileName const tmp_ret = FileName::tempName("lyxauto");
2399         if (!tmp_ret.empty()) {
2400                 buffer_.writeFile(tmp_ret);
2401                 // assume successful write of tmp_ret
2402                 if (!tmp_ret.moveTo(fname_))
2403                         failed = true;
2404         } else
2405                 failed = true;
2406
2407         if (failed) {
2408                 // failed to write/rename tmp_ret so try writing direct
2409                 if (!buffer_.writeFile(fname_)) {
2410                         // It is dangerous to do this in the child,
2411                         // but safe in the parent, so...
2412                         if (pid == -1) // emit message signal.
2413                                 buffer_.message(_("Autosave failed!"));
2414                 }
2415         }
2416
2417         if (pid == 0) // we are the child so...
2418                 _exit(0);
2419
2420         return pid;
2421 }
2422
2423 } // namespace anon
2424
2425
2426 // Perfect target for a thread...
2427 void Buffer::autoSave() const
2428 {
2429         if (isBakClean() || isReadonly()) {
2430                 // We don't save now, but we'll try again later
2431                 resetAutosaveTimers();
2432                 return;
2433         }
2434
2435         // emit message signal.
2436         message(_("Autosaving current document..."));
2437
2438         // create autosave filename
2439         string fname = filePath();
2440         fname += '#';
2441         fname += d->filename.onlyFileName();
2442         fname += '#';
2443
2444         AutoSaveBuffer autosave(*this, FileName(fname));
2445         autosave.start();
2446
2447         markBakClean();
2448         resetAutosaveTimers();
2449 }
2450
2451
2452 string Buffer::bufferFormat() const
2453 {
2454         if (isDocBook())
2455                 return "docbook";
2456         if (isLiterate())
2457                 return "literate";
2458         if (params().encoding().package() == Encoding::japanese)
2459                 return "platex";
2460         return "latex";
2461 }
2462
2463
2464 bool Buffer::doExport(string const & format, bool put_in_tempdir,
2465         string & result_file) const
2466 {
2467         string backend_format;
2468         OutputParams runparams(&params().encoding());
2469         runparams.flavor = OutputParams::LATEX;
2470         runparams.linelen = lyxrc.plaintext_linelen;
2471         vector<string> backs = backends();
2472         if (find(backs.begin(), backs.end(), format) == backs.end()) {
2473                 // Get shortest path to format
2474                 Graph::EdgePath path;
2475                 for (vector<string>::const_iterator it = backs.begin();
2476                      it != backs.end(); ++it) {
2477                         Graph::EdgePath p = theConverters().getPath(*it, format);
2478                         if (!p.empty() && (path.empty() || p.size() < path.size())) {
2479                                 backend_format = *it;
2480                                 path = p;
2481                         }
2482                 }
2483                 if (!path.empty())
2484                         runparams.flavor = theConverters().getFlavor(path);
2485                 else {
2486                         Alert::error(_("Couldn't export file"),
2487                                 bformat(_("No information for exporting the format %1$s."),
2488                                    formats.prettyName(format)));
2489                         return false;
2490                 }
2491         } else {
2492                 backend_format = format;
2493                 // FIXME: Don't hardcode format names here, but use a flag
2494                 if (backend_format == "pdflatex")
2495                         runparams.flavor = OutputParams::PDFLATEX;
2496         }
2497
2498         string filename = latexName(false);
2499         filename = addName(temppath(), filename);
2500         filename = changeExtension(filename,
2501                                    formats.extension(backend_format));
2502
2503         // fix macros
2504         updateMacroInstances();
2505
2506         // Plain text backend
2507         if (backend_format == "text")
2508                 writePlaintextFile(*this, FileName(filename), runparams);
2509         // no backend
2510         else if (backend_format == "lyx")
2511                 writeFile(FileName(filename));
2512         // Docbook backend
2513         else if (isDocBook()) {
2514                 runparams.nice = !put_in_tempdir;
2515                 makeDocBookFile(FileName(filename), runparams);
2516         }
2517         // LaTeX backend
2518         else if (backend_format == format) {
2519                 runparams.nice = true;
2520                 if (!makeLaTeXFile(FileName(filename), string(), runparams))
2521                         return false;
2522         } else if (!lyxrc.tex_allows_spaces
2523                    && contains(filePath(), ' ')) {
2524                 Alert::error(_("File name error"),
2525                            _("The directory path to the document cannot contain spaces."));
2526                 return false;
2527         } else {
2528                 runparams.nice = false;
2529                 if (!makeLaTeXFile(FileName(filename), filePath(), runparams))
2530                         return false;
2531         }
2532
2533         string const error_type = (format == "program")
2534                 ? "Build" : bufferFormat();
2535         ErrorList & error_list = d->errorLists[error_type];
2536         string const ext = formats.extension(format);
2537         FileName const tmp_result_file(changeExtension(filename, ext));
2538         bool const success = theConverters().convert(this, FileName(filename),
2539                 tmp_result_file, FileName(absFileName()), backend_format, format,
2540                 error_list);
2541         // Emit the signal to show the error list.
2542         if (format != backend_format)
2543                 errors(error_type);
2544         if (!success)
2545                 return false;
2546
2547         if (put_in_tempdir) {
2548                 result_file = tmp_result_file.absFilename();
2549                 return true;
2550         }
2551
2552         result_file = changeExtension(absFileName(), ext);
2553         // We need to copy referenced files (e. g. included graphics
2554         // if format == "dvi") to the result dir.
2555         vector<ExportedFile> const files =
2556                 runparams.exportdata->externalFiles(format);
2557         string const dest = onlyPath(result_file);
2558         CopyStatus status = SUCCESS;
2559         for (vector<ExportedFile>::const_iterator it = files.begin();
2560                 it != files.end() && status != CANCEL; ++it) {
2561                 string const fmt = formats.getFormatFromFile(it->sourceName);
2562                 status = copyFile(fmt, it->sourceName,
2563                         makeAbsPath(it->exportName, dest),
2564                         it->exportName, status == FORCE);
2565         }
2566         if (status == CANCEL) {
2567                 message(_("Document export cancelled."));
2568         } else if (tmp_result_file.exists()) {
2569                 // Finally copy the main file
2570                 status = copyFile(format, tmp_result_file,
2571                         FileName(result_file), result_file,
2572                         status == FORCE);
2573                 message(bformat(_("Document exported as %1$s "
2574                         "to file `%2$s'"),
2575                         formats.prettyName(format),
2576                         makeDisplayPath(result_file)));
2577         } else {
2578                 // This must be a dummy converter like fax (bug 1888)
2579                 message(bformat(_("Document exported as %1$s"),
2580                         formats.prettyName(format)));
2581         }
2582
2583         return true;
2584 }
2585
2586
2587 bool Buffer::doExport(string const & format, bool put_in_tempdir) const
2588 {
2589         string result_file;
2590         return doExport(format, put_in_tempdir, result_file);
2591 }
2592
2593
2594 bool Buffer::preview(string const & format) const
2595 {
2596         string result_file;
2597         if (!doExport(format, true, result_file))
2598                 return false;
2599         return formats.view(*this, FileName(result_file), format);
2600 }
2601
2602
2603 bool Buffer::isExportable(string const & format) const
2604 {
2605         vector<string> backs = backends();
2606         for (vector<string>::const_iterator it = backs.begin();
2607              it != backs.end(); ++it)
2608                 if (theConverters().isReachable(*it, format))
2609                         return true;
2610         return false;
2611 }
2612
2613
2614 vector<Format const *> Buffer::exportableFormats(bool only_viewable) const
2615 {
2616         vector<string> backs = backends();
2617         vector<Format const *> result =
2618                 theConverters().getReachable(backs[0], only_viewable, true);
2619         for (vector<string>::const_iterator it = backs.begin() + 1;
2620              it != backs.end(); ++it) {
2621                 vector<Format const *>  r =
2622                         theConverters().getReachable(*it, only_viewable, false);
2623                 result.insert(result.end(), r.begin(), r.end());
2624         }
2625         return result;
2626 }
2627
2628
2629 vector<string> Buffer::backends() const
2630 {
2631         vector<string> v;
2632         if (params().baseClass()->isTeXClassAvailable()) {
2633                 v.push_back(bufferFormat());
2634                 // FIXME: Don't hardcode format names here, but use a flag
2635                 if (v.back() == "latex")
2636                         v.push_back("pdflatex");
2637         }
2638         v.push_back("text");
2639         v.push_back("lyx");
2640         return v;
2641 }
2642
2643
2644 bool Buffer::readFileHelper(FileName const & s)
2645 {
2646         // File information about normal file
2647         if (!s.exists()) {
2648                 docstring const file = makeDisplayPath(s.absFilename(), 50);
2649                 docstring text = bformat(_("The specified document\n%1$s"
2650                                                      "\ncould not be read."), file);
2651                 Alert::error(_("Could not read document"), text);
2652                 return false;
2653         }
2654
2655         // Check if emergency save file exists and is newer.
2656         FileName const e(s.absFilename() + ".emergency");
2657
2658         if (e.exists() && s.exists() && e.lastModified() > s.lastModified()) {
2659                 docstring const file = makeDisplayPath(s.absFilename(), 20);
2660                 docstring const text =
2661                         bformat(_("An emergency save of the document "
2662                                   "%1$s exists.\n\n"
2663                                                "Recover emergency save?"), file);
2664                 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
2665                                       _("&Recover"),  _("&Load Original"),
2666                                       _("&Cancel")))
2667                 {
2668                 case 0:
2669                         // the file is not saved if we load the emergency file.
2670                         markDirty();
2671                         return readFile(e);
2672                 case 1:
2673                         break;
2674                 default:
2675                         return false;
2676                 }
2677         }
2678
2679         // Now check if autosave file is newer.
2680         FileName const a(onlyPath(s.absFilename()) + '#' + onlyFilename(s.absFilename()) + '#');
2681
2682         if (a.exists() && s.exists() && a.lastModified() > s.lastModified()) {
2683                 docstring const file = makeDisplayPath(s.absFilename(), 20);
2684                 docstring const text =
2685                         bformat(_("The backup of the document "
2686                                   "%1$s is newer.\n\nLoad the "
2687                                                "backup instead?"), file);
2688                 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
2689                                       _("&Load backup"), _("Load &original"),
2690                                       _("&Cancel") ))
2691                 {
2692                 case 0:
2693                         // the file is not saved if we load the autosave file.
2694                         markDirty();
2695                         return readFile(a);
2696                 case 1:
2697                         // Here we delete the autosave
2698                         a.removeFile();
2699                         break;
2700                 default:
2701                         return false;
2702                 }
2703         }
2704         return readFile(s);
2705 }
2706
2707
2708 bool Buffer::loadLyXFile(FileName const & s)
2709 {
2710         if (s.isReadableFile()) {
2711                 if (readFileHelper(s)) {
2712                         lyxvc().file_found_hook(s);
2713                         if (!s.isWritable())
2714                                 setReadonly(true);
2715                         return true;
2716                 }
2717         } else {
2718                 docstring const file = makeDisplayPath(s.absFilename(), 20);
2719                 // Here we probably should run
2720                 if (LyXVC::file_not_found_hook(s)) {
2721                         docstring const text =
2722                                 bformat(_("Do you want to retrieve the document"
2723                                                        " %1$s from version control?"), file);
2724                         int const ret = Alert::prompt(_("Retrieve from version control?"),
2725                                 text, 0, 1, _("&Retrieve"), _("&Cancel"));
2726
2727                         if (ret == 0) {
2728                                 // How can we know _how_ to do the checkout?
2729                                 // With the current VC support it has to be,
2730                                 // a RCS file since CVS do not have special ,v files.
2731                                 RCS::retrieve(s);
2732                                 return loadLyXFile(s);
2733                         }
2734                 }
2735         }
2736         return false;
2737 }
2738
2739
2740 void Buffer::bufferErrors(TeXErrors const & terr, ErrorList & errorList) const
2741 {
2742         TeXErrors::Errors::const_iterator cit = terr.begin();
2743         TeXErrors::Errors::const_iterator end = terr.end();
2744
2745         for (; cit != end; ++cit) {
2746                 int id_start = -1;
2747                 int pos_start = -1;
2748                 int errorRow = cit->error_in_line;
2749                 bool found = d->texrow.getIdFromRow(errorRow, id_start,
2750                                                        pos_start);
2751                 int id_end = -1;
2752                 int pos_end = -1;
2753                 do {
2754                         ++errorRow;
2755                         found = d->texrow.getIdFromRow(errorRow, id_end, pos_end);
2756                 } while (found && id_start == id_end && pos_start == pos_end);
2757
2758                 errorList.push_back(ErrorItem(cit->error_desc,
2759                         cit->error_text, id_start, pos_start, pos_end));
2760         }
2761 }
2762
2763
2764 void Buffer::setBuffersForInsets() const
2765 {
2766         inset().setBuffer(const_cast<Buffer &>(*this)); 
2767 }
2768
2769
2770 void Buffer::updateLabels(UpdateScope scope) const
2771 {
2772         // Use the master text class also for child documents
2773         Buffer const * const master = masterBuffer();
2774         DocumentClass const & textclass = master->params().documentClass();
2775
2776         // keep the buffers to be children in this set. If the call from the
2777         // master comes back we can see which of them were actually seen (i.e.
2778         // via an InsetInclude). The remaining ones in the set need still be updated.
2779         static std::set<Buffer const *> bufToUpdate;
2780         if (scope == UpdateMaster) {
2781                 // If this is a child document start with the master
2782                 if (master != this) {
2783                         bufToUpdate.insert(this);
2784                         master->updateLabels();
2785                         // Do this here in case the master has no gui associated with it. Then, 
2786                         // the TocModel is not updated and TocModel::toc_ is invalid (bug 5699).
2787                         if (!master->gui_)
2788                                 structureChanged();     
2789
2790                         // was buf referenced from the master (i.e. not in bufToUpdate anymore)?
2791                         if (bufToUpdate.find(this) == bufToUpdate.end())
2792                                 return;
2793                 }
2794
2795                 // start over the counters in the master
2796                 textclass.counters().reset();
2797         }
2798
2799         // update will be done below for this buffer
2800         bufToUpdate.erase(this);
2801
2802         // update all caches
2803         clearReferenceCache();
2804         updateMacros();
2805
2806         Buffer & cbuf = const_cast<Buffer &>(*this);
2807
2808         LASSERT(!text().paragraphs().empty(), /**/);
2809
2810         // do the real work
2811         ParIterator parit = cbuf.par_iterator_begin();
2812         updateLabels(parit);
2813
2814         if (master != this)
2815                 // TocBackend update will be done later.
2816                 return;
2817
2818         cbuf.tocBackend().update();
2819         if (scope == UpdateMaster)
2820                 cbuf.structureChanged();
2821 }
2822
2823
2824 static depth_type getDepth(DocIterator const & it)
2825 {
2826         depth_type depth = 0;
2827         for (size_t i = 0 ; i < it.depth() ; ++i)
2828                 if (!it[i].inset().inMathed())
2829                         depth += it[i].paragraph().getDepth() + 1;
2830         // remove 1 since the outer inset does not count
2831         return depth - 1;
2832 }
2833
2834 static depth_type getItemDepth(ParIterator const & it)
2835 {
2836         Paragraph const & par = *it;
2837         LabelType const labeltype = par.layout().labeltype;
2838
2839         if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
2840                 return 0;
2841
2842         // this will hold the lowest depth encountered up to now.
2843         depth_type min_depth = getDepth(it);
2844         ParIterator prev_it = it;
2845         while (true) {
2846                 if (prev_it.pit())
2847                         --prev_it.top().pit();
2848                 else {
2849                         // start of nested inset: go to outer par
2850                         prev_it.pop_back();
2851                         if (prev_it.empty()) {
2852                                 // start of document: nothing to do
2853                                 return 0;
2854                         }
2855                 }
2856
2857                 // We search for the first paragraph with same label
2858                 // that is not more deeply nested.
2859                 Paragraph & prev_par = *prev_it;
2860                 depth_type const prev_depth = getDepth(prev_it);
2861                 if (labeltype == prev_par.layout().labeltype) {
2862                         if (prev_depth < min_depth)
2863                                 return prev_par.itemdepth + 1;
2864                         if (prev_depth == min_depth)
2865                                 return prev_par.itemdepth;
2866                 }
2867                 min_depth = min(min_depth, prev_depth);
2868                 // small optimization: if we are at depth 0, we won't
2869                 // find anything else
2870                 if (prev_depth == 0)
2871                         return 0;
2872         }
2873 }
2874
2875
2876 static bool needEnumCounterReset(ParIterator const & it)
2877 {
2878         Paragraph const & par = *it;
2879         LASSERT(par.layout().labeltype == LABEL_ENUMERATE, /**/);
2880         depth_type const cur_depth = par.getDepth();
2881         ParIterator prev_it = it;
2882         while (prev_it.pit()) {
2883                 --prev_it.top().pit();
2884                 Paragraph const & prev_par = *prev_it;
2885                 if (prev_par.getDepth() <= cur_depth)
2886                         return  prev_par.layout().labeltype != LABEL_ENUMERATE;
2887         }
2888         // start of nested inset: reset
2889         return true;
2890 }
2891
2892
2893 // set the label of a paragraph. This includes the counters.
2894 static void setLabel(Buffer const & buf, ParIterator & it)
2895 {
2896         BufferParams const & bp = buf.masterBuffer()->params();
2897         DocumentClass const & textclass = bp.documentClass();
2898         Paragraph & par = it.paragraph();
2899         Layout const & layout = par.layout();
2900         Counters & counters = textclass.counters();
2901
2902         if (par.params().startOfAppendix()) {
2903                 // FIXME: only the counter corresponding to toplevel
2904                 // sectionning should be reset
2905                 counters.reset();
2906                 counters.appendix(true);
2907         }
2908         par.params().appendix(counters.appendix());
2909
2910         // Compute the item depth of the paragraph
2911         par.itemdepth = getItemDepth(it);
2912
2913         if (layout.margintype == MARGIN_MANUAL) {
2914                 if (par.params().labelWidthString().empty())
2915                         par.params().labelWidthString(par.translateIfPossible(layout.labelstring(), bp));
2916         } else {
2917                 par.params().labelWidthString(docstring());
2918         }
2919
2920         switch(layout.labeltype) {
2921         case LABEL_COUNTER:
2922                 if (layout.toclevel <= bp.secnumdepth
2923                     && (layout.latextype != LATEX_ENVIRONMENT
2924                         || isFirstInSequence(it.pit(), it.plist()))) {
2925                         counters.step(layout.counter);
2926                         par.params().labelString(
2927                                 par.expandLabel(layout, bp));
2928                 } else
2929                         par.params().labelString(docstring());
2930                 break;
2931
2932         case LABEL_ITEMIZE: {
2933                 // At some point of time we should do something more
2934                 // clever here, like:
2935                 //   par.params().labelString(
2936                 //    bp.user_defined_bullet(par.itemdepth).getText());
2937                 // for now, use a simple hardcoded label
2938                 docstring itemlabel;
2939                 switch (par.itemdepth) {
2940                 case 0:
2941                         itemlabel = char_type(0x2022);
2942                         break;
2943                 case 1:
2944                         itemlabel = char_type(0x2013);
2945                         break;
2946                 case 2:
2947                         itemlabel = char_type(0x2217);
2948                         break;
2949                 case 3:
2950                         itemlabel = char_type(0x2219); // or 0x00b7
2951                         break;
2952                 }
2953                 par.params().labelString(itemlabel);
2954                 break;
2955         }
2956
2957         case LABEL_ENUMERATE: {
2958                 // FIXME: Yes I know this is a really, really! bad solution
2959                 // (Lgb)
2960                 docstring enumcounter = from_ascii("enum");
2961
2962                 switch (par.itemdepth) {
2963                 case 2:
2964                         enumcounter += 'i';
2965                 case 1:
2966                         enumcounter += 'i';
2967                 case 0:
2968                         enumcounter += 'i';
2969                         break;
2970                 case 3:
2971                         enumcounter += "iv";
2972                         break;
2973                 default:
2974                         // not a valid enumdepth...
2975                         break;
2976                 }
2977
2978                 // Maybe we have to reset the enumeration counter.
2979                 if (needEnumCounterReset(it))
2980                         counters.reset(enumcounter);
2981
2982                 counters.step(enumcounter);
2983
2984                 string format;
2985
2986                 switch (par.itemdepth) {
2987                 case 0:
2988                         format = N_("\\arabic{enumi}.");
2989                         break;
2990                 case 1:
2991                         format = N_("(\\alph{enumii})");
2992                         break;
2993                 case 2:
2994                         format = N_("\\roman{enumiii}.");
2995                         break;
2996                 case 3:
2997                         format = N_("\\Alph{enumiv}.");
2998                         break;
2999                 default:
3000                         // not a valid enumdepth...
3001                         break;
3002                 }
3003
3004                 par.params().labelString(counters.counterLabel(
3005                         par.translateIfPossible(from_ascii(format), bp)));
3006
3007                 break;
3008         }
3009
3010         case LABEL_SENSITIVE: {
3011                 string const & type = counters.current_float();
3012                 docstring full_label;
3013                 if (type.empty())
3014                         full_label = buf.B_("Senseless!!! ");
3015                 else {
3016                         docstring name = buf.B_(textclass.floats().getType(type).name());
3017                         if (counters.hasCounter(from_utf8(type))) {
3018                                 counters.step(from_utf8(type));
3019                                 full_label = bformat(from_ascii("%1$s %2$s:"), 
3020                                                      name, 
3021                                                      counters.theCounter(from_utf8(type)));
3022                         } else
3023                                 full_label = bformat(from_ascii("%1$s #:"), name);      
3024                 }
3025                 par.params().labelString(full_label);   
3026                 break;
3027         }
3028
3029         case LABEL_NO_LABEL:
3030                 par.params().labelString(docstring());
3031                 break;
3032
3033         case LABEL_MANUAL:
3034         case LABEL_TOP_ENVIRONMENT:
3035         case LABEL_CENTERED_TOP_ENVIRONMENT:
3036         case LABEL_STATIC:      
3037         case LABEL_BIBLIO:
3038                 par.params().labelString(
3039                         par.translateIfPossible(layout.labelstring(), bp));
3040                 break;
3041         }
3042 }
3043
3044
3045 void Buffer::updateLabels(ParIterator & parit) const
3046 {
3047         LASSERT(parit.pit() == 0, /**/);
3048
3049         // set the position of the text in the buffer to be able
3050         // to resolve macros in it. This has nothing to do with
3051         // labels, but by putting it here we avoid implementing
3052         // a whole bunch of traversal routines just for this call.
3053         parit.text()->setMacrocontextPosition(parit);
3054
3055         depth_type maxdepth = 0;
3056         pit_type const lastpit = parit.lastpit();
3057         for ( ; parit.pit() <= lastpit ; ++parit.pit()) {
3058                 // reduce depth if necessary
3059                 parit->params().depth(min(parit->params().depth(), maxdepth));
3060                 maxdepth = parit->getMaxDepthAfter();
3061
3062                 // set the counter for this paragraph
3063                 setLabel(*this, parit);
3064
3065                 // Now the insets
3066                 InsetList::const_iterator iit = parit->insetList().begin();
3067                 InsetList::const_iterator end = parit->insetList().end();
3068                 for (; iit != end; ++iit) {
3069                         parit.pos() = iit->pos;
3070                         iit->inset->updateLabels(parit);
3071                 }
3072         }
3073 }
3074
3075
3076 bool Buffer::nextWord(DocIterator & from, DocIterator & to,
3077         docstring & word) const
3078 {
3079         bool inword = false;
3080         bool ignoreword = false;
3081         string lang_code;
3082         to = from;
3083
3084         while (to.depth()) {
3085                 if (isLetter(to)) {
3086                         if (!inword) {
3087                                 inword = true;
3088                                 ignoreword = false;
3089                                 from = to;
3090                                 word.clear();
3091                                 lang_code = to.paragraph().getFontSettings(params(),
3092                                         to.pos()).language()->code();
3093                         }
3094                         // Insets like optional hyphens and ligature
3095                         // break are part of a word.
3096                         if (!to.paragraph().isInset(to.pos())) {
3097                                 char_type const c = to.paragraph().getChar(to.pos());
3098                                 word += c;
3099                                 if (isDigit(c))
3100                                         ignoreword = true;
3101                         }
3102                 } else { // !isLetter(cur)
3103                         if (inword && !word.empty() && !ignoreword)
3104                                 return true;
3105                         inword = false;
3106                 }
3107                 to.forwardPos();
3108         }
3109
3110         return false;
3111 }
3112
3113 } // namespace lyx