2 * \file buffer_funcs.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Alfredo Braunstein
9 * Full author contact details are available in file CREDITS.
15 #include "buffer_funcs.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
20 #include "DocIterator.h"
22 #include "ErrorList.h"
24 #include "FloatList.h"
26 #include "InsetIterator.h"
31 #include "lyxlayout_ptr_fwd.h"
32 #include "TextClass.h"
33 #include "TextClassList.h"
34 #include "Paragraph.h"
35 #include "paragraph_funcs.h"
36 #include "ParagraphList.h"
37 #include "ParagraphParameters.h"
38 #include "ParIterator.h"
42 #include "TocBackend.h"
43 #include "VCBackend.h"
45 #include "frontends/alert.h"
47 #include "insets/InsetBibitem.h"
48 #include "insets/InsetInclude.h"
50 #include "support/filetools.h"
51 #include "support/fs_extras.h"
52 #include "support/lyxlib.h"
54 #include <boost/bind.hpp>
55 #include <boost/filesystem/operations.hpp>
65 using support::bformat;
66 using support::FileName;
67 using support::libFileSearch;
68 using support::makeAbsPath;
69 using support::makeDisplayPath;
70 using support::onlyFilename;
71 using support::onlyPath;
72 using support::unlink;
74 namespace Alert = frontend::Alert;
75 namespace fs = boost::filesystem;
79 bool readFile(Buffer * const b, FileName const & s)
83 // File information about normal file
84 if (!fs::exists(s.toFilesystemEncoding())) {
85 docstring const file = makeDisplayPath(s.absFilename(), 50);
86 docstring text = bformat(_("The specified document\n%1$s"
87 "\ncould not be read."), file);
88 Alert::error(_("Could not read document"), text);
92 // Check if emergency save file exists and is newer.
93 FileName const e(s.absFilename() + ".emergency");
95 if (fs::exists(e.toFilesystemEncoding()) &&
96 fs::exists(s.toFilesystemEncoding()) &&
97 fs::last_write_time(e.toFilesystemEncoding()) > fs::last_write_time(s.toFilesystemEncoding()))
99 docstring const file = makeDisplayPath(s.absFilename(), 20);
100 docstring const text =
101 bformat(_("An emergency save of the document "
103 "Recover emergency save?"), file);
104 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
105 _("&Recover"), _("&Load Original"),
109 // the file is not saved if we load the emergency file.
111 return b->readFile(e);
119 // Now check if autosave file is newer.
120 FileName const a(onlyPath(s.absFilename()) + '#' + onlyFilename(s.absFilename()) + '#');
122 if (fs::exists(a.toFilesystemEncoding()) &&
123 fs::exists(s.toFilesystemEncoding()) &&
124 fs::last_write_time(a.toFilesystemEncoding()) > fs::last_write_time(s.toFilesystemEncoding()))
126 docstring const file = makeDisplayPath(s.absFilename(), 20);
127 docstring const text =
128 bformat(_("The backup of the document "
129 "%1$s is newer.\n\nLoad the "
130 "backup instead?"), file);
131 switch (Alert::prompt(_("Load backup?"), text, 0, 2,
132 _("&Load backup"), _("Load &original"),
136 // the file is not saved if we load the autosave file.
138 return b->readFile(a);
140 // Here we delete the autosave
147 return b->readFile(s);
155 bool loadLyXFile(Buffer * b, FileName const & s)
159 if (fs::is_readable(s.toFilesystemEncoding())) {
160 if (readFile(b, s)) {
161 b->lyxvc().file_found_hook(s);
162 if (!fs::is_writable(s.toFilesystemEncoding()))
163 b->setReadonly(true);
167 docstring const file = makeDisplayPath(s.absFilename(), 20);
168 // Here we probably should run
169 if (LyXVC::file_not_found_hook(s)) {
170 docstring const text =
171 bformat(_("Do you want to retrieve the document"
172 " %1$s from version control?"), file);
173 int const ret = Alert::prompt(_("Retrieve from version control?"),
174 text, 0, 1, _("&Retrieve"), _("&Cancel"));
177 // How can we know _how_ to do the checkout?
178 // With the current VC support it has to be,
179 // a RCS file since CVS do not have special ,v files.
181 return loadLyXFile(b, s);
189 bool checkIfLoaded(FileName const & fn)
191 return theBufferList().getBuffer(fn.absFilename());
195 Buffer * checkAndLoadLyXFile(FileName const & filename)
197 // File already open?
198 Buffer * checkBuffer = theBufferList().getBuffer(filename.absFilename());
200 if (checkBuffer->isClean())
202 docstring const file = makeDisplayPath(filename.absFilename(), 20);
203 docstring text = bformat(_(
204 "The document %1$s is already loaded and has unsaved changes.\n"
205 "Do you want to abandon your changes and reload the version on disk?"), file);
206 if (Alert::prompt(_("Reload saved document?"),
207 text, 0, 1, _("&Reload"), _("&Keep Changes")))
210 // FIXME: should be LFUN_REVERT
211 if (theBufferList().close(checkBuffer, false))
213 return checkAndLoadLyXFile(filename);
215 // The file could not be closed.
219 if (isFileReadable(filename)) {
220 Buffer * b = theBufferList().newBuffer(filename.absFilename());
221 if (!lyx::loadLyXFile(b, filename)) {
222 theBufferList().release(b);
228 docstring text = bformat(_("The document %1$s does not yet "
229 "exist.\n\nDo you want to create a new document?"),
230 from_utf8(filename.absFilename()));
231 if (!Alert::prompt(_("Create new document?"),
232 text, 0, 1, _("&Create"), _("Cancel")))
233 return newFile(filename.absFilename(), string(), true);
238 // FIXME newFile() should probably be a member method of Application...
239 Buffer * newFile(string const & filename, string const & templatename,
243 Buffer * b = theBufferList().newBuffer(filename);
247 // use defaults.lyx as a default template if it exists.
248 if (templatename.empty())
249 tname = libFileSearch("templates", "defaults.lyx");
251 tname = makeAbsPath(templatename);
253 if (!tname.empty()) {
254 if (!b->readFile(tname)) {
255 docstring const file = makeDisplayPath(tname.absFilename(), 50);
256 docstring const text = bformat(
257 _("The specified document template\n%1$s\ncould not be read."),
259 Alert::error(_("Could not read template"), text);
260 theBufferList().release(b);
267 b->setFileName(filename);
270 b->setReadonly(false);
271 b->fully_loaded(true);
277 void bufferErrors(Buffer const & buf, TeXErrors const & terr,
278 ErrorList & errorList)
280 TeXErrors::Errors::const_iterator cit = terr.begin();
281 TeXErrors::Errors::const_iterator end = terr.end();
283 for (; cit != end; ++cit) {
286 int errorrow = cit->error_in_line;
287 bool found = buf.texrow().getIdFromRow(errorrow, id_start,
293 found = buf.texrow().getIdFromRow(errorrow, id_end,
295 } while (found && id_start == id_end && pos_start == pos_end);
297 errorList.push_back(ErrorItem(cit->error_desc,
298 cit->error_text, id_start, pos_start, pos_end));
303 string const bufferFormat(Buffer const & buffer)
305 if (buffer.isDocBook())
307 else if (buffer.isLiterate())
314 int countWords(DocIterator const & from, DocIterator const & to)
318 for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
319 // Copied and adapted from isLetter() in ControlSpellChecker
321 && dit.pos() != dit.lastpos()
322 && dit.paragraph().isLetter(dit.pos())
323 && !dit.paragraph().isDeleted(dit.pos())) {
338 depth_type getDepth(DocIterator const & it)
340 depth_type depth = 0;
341 for (size_t i = 0 ; i < it.depth() ; ++i)
342 if (!it[i].inset().inMathed())
343 depth += it[i].paragraph().getDepth() + 1;
344 // remove 1 since the outer inset does not count
348 depth_type getItemDepth(ParIterator const & it)
350 Paragraph const & par = *it;
351 LabelType const labeltype = par.layout()->labeltype;
353 if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
356 // this will hold the lowest depth encountered up to now.
357 depth_type min_depth = getDepth(it);
358 ParIterator prev_it = it;
361 --prev_it.top().pit();
363 // start of nested inset: go to outer par
365 if (prev_it.empty()) {
366 // start of document: nothing to do
371 // We search for the first paragraph with same label
372 // that is not more deeply nested.
373 Paragraph & prev_par = *prev_it;
374 depth_type const prev_depth = getDepth(prev_it);
375 if (labeltype == prev_par.layout()->labeltype) {
376 if (prev_depth < min_depth) {
377 return prev_par.itemdepth + 1;
379 else if (prev_depth == min_depth) {
380 return prev_par.itemdepth;
383 min_depth = std::min(min_depth, prev_depth);
384 // small optimization: if we are at depth 0, we won't
385 // find anything else
386 if (prev_depth == 0) {
393 bool needEnumCounterReset(ParIterator const & it)
395 Paragraph const & par = *it;
396 BOOST_ASSERT(par.layout()->labeltype == LABEL_ENUMERATE);
397 depth_type const cur_depth = par.getDepth();
398 ParIterator prev_it = it;
399 while (prev_it.pit()) {
400 --prev_it.top().pit();
401 Paragraph const & prev_par = *prev_it;
402 if (prev_par.getDepth() <= cur_depth)
403 return prev_par.layout()->labeltype != LABEL_ENUMERATE;
405 // start of nested inset: reset
410 // set the label of a paragraph. This includes the counters.
411 void setLabel(Buffer const & buf, ParIterator & it)
413 TextClass const & textclass = buf.params().getTextClass();
414 Paragraph & par = it.paragraph();
415 LayoutPtr const & layout = par.layout();
416 Counters & counters = textclass.counters();
418 if (par.params().startOfAppendix()) {
419 // FIXME: only the counter corresponding to toplevel
420 // sectionning should be reset
422 counters.appendix(true);
424 par.params().appendix(counters.appendix());
426 // Compute the item depth of the paragraph
427 par.itemdepth = getItemDepth(it);
429 if (layout->margintype == MARGIN_MANUAL) {
430 if (par.params().labelWidthString().empty())
431 par.params().labelWidthString(par.translateIfPossible(layout->labelstring(), buf.params()));
433 par.params().labelWidthString(docstring());
436 switch(layout->labeltype) {
438 if (layout->toclevel <= buf.params().secnumdepth
439 && (layout->latextype != LATEX_ENVIRONMENT
440 || isFirstInSequence(it.pit(), it.plist()))) {
441 counters.step(layout->counter);
442 par.params().labelString(
443 par.expandLabel(layout, buf.params()));
445 par.params().labelString(docstring());
448 case LABEL_ITEMIZE: {
449 // At some point of time we should do something more
450 // clever here, like:
451 // par.params().labelString(
452 // buf.params().user_defined_bullet(par.itemdepth).getText());
453 // for now, use a simple hardcoded label
455 switch (par.itemdepth) {
457 itemlabel = char_type(0x2022);
460 itemlabel = char_type(0x2013);
463 itemlabel = char_type(0x2217);
466 itemlabel = char_type(0x2219); // or 0x00b7
469 par.params().labelString(itemlabel);
473 case LABEL_ENUMERATE: {
474 // FIXME: Yes I know this is a really, really! bad solution
476 docstring enumcounter = from_ascii("enum");
478 switch (par.itemdepth) {
490 // not a valid enumdepth...
494 // Maybe we have to reset the enumeration counter.
495 if (needEnumCounterReset(it))
496 counters.reset(enumcounter);
498 counters.step(enumcounter);
502 switch (par.itemdepth) {
504 format = N_("\\arabic{enumi}.");
507 format = N_("(\\alph{enumii})");
510 format = N_("\\roman{enumiii}.");
513 format = N_("\\Alph{enumiv}.");
516 // not a valid enumdepth...
520 par.params().labelString(counters.counterLabel(
521 par.translateIfPossible(from_ascii(format), buf.params())));
526 case LABEL_SENSITIVE: {
527 string const & type = counters.current_float();
528 docstring full_label;
530 full_label = buf.B_("Senseless!!! ");
532 docstring name = buf.B_(textclass.floats().getType(type).name());
533 if (counters.hasCounter(from_utf8(type))) {
534 counters.step(from_utf8(type));
535 full_label = bformat(from_ascii("%1$s %2$s:"),
537 counters.theCounter(from_utf8(type)));
539 full_label = bformat(from_ascii("%1$s #:"), name);
541 par.params().labelString(full_label);
546 par.params().labelString(docstring());
550 case LABEL_TOP_ENVIRONMENT:
551 case LABEL_CENTERED_TOP_ENVIRONMENT:
554 par.params().labelString(
555 par.translateIfPossible(layout->labelstring(),
563 void updateLabels(Buffer const & buf, ParIterator & parit)
565 BOOST_ASSERT(parit.pit() == 0);
567 depth_type maxdepth = 0;
568 pit_type const lastpit = parit.lastpit();
569 for ( ; parit.pit() <= lastpit ; ++parit.pit()) {
570 // reduce depth if necessary
571 parit->params().depth(min(parit->params().depth(), maxdepth));
572 maxdepth = parit->getMaxDepthAfter();
574 // set the counter for this paragraph
575 setLabel(buf, parit);
578 InsetList::const_iterator iit = parit->insetlist.begin();
579 InsetList::const_iterator end = parit->insetlist.end();
580 for (; iit != end; ++iit) {
581 parit.pos() = iit->pos;
582 iit->inset->updateLabels(buf, parit);
589 // FIXME: buf should should be const because updateLabels() modifies
590 // the contents of the paragraphs.
591 void updateLabels(Buffer const & buf, bool childonly)
593 Buffer const * const master = buf.getMasterBuffer();
594 // Use the master text class also for child documents
595 TextClass const & textclass = master->params().getTextClass();
598 // If this is a child document start with the master
599 if (master != &buf) {
600 updateLabels(*master);
604 // start over the counters
605 textclass.counters().reset();
608 Buffer & cbuf = const_cast<Buffer &>(buf);
610 if (buf.text().empty()) {
611 // FIXME: we don't call continue with updateLabels()
612 // here because it crashes on newly created documents.
613 // But the TocBackend needs to be initialised
614 // nonetheless so we update the tocBackend manually.
615 cbuf.tocBackend().update();
620 ParIterator parit = par_iterator_begin(buf.inset());
621 updateLabels(buf, parit);
623 cbuf.tocBackend().update();
625 cbuf.structureChanged();
627 // the embedding signal is emitted with structureChanged signal
628 // this is inaccurate so these two will be separated later.
629 //cbuf.embeddedFiles().update();
630 //cbuf.embeddingChanged();
634 void checkBufferStructure(Buffer & buffer, ParIterator const & par_it)
636 if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
637 Buffer * master = buffer.getMasterBuffer();
638 master->tocBackend().updateItem(par_it);
639 master->structureChanged();
643 textclass_type defaultTextclass()
645 // We want to return the article class. if `first' is
646 // true in the returned pair, then `second' is the textclass
647 // number; if it is false, second is 0. In both cases, second
649 return textclasslist.numberOfClass("article").second;
653 void loadChildDocuments(Buffer const & buf)
655 bool parse_error = false;
657 for (InsetIterator it = inset_iterator_begin(buf.inset()); it; ++it) {
658 if (it->lyxCode() != INCLUDE_CODE)
660 InsetInclude const & inset = static_cast<InsetInclude const &>(*it);
661 InsetCommandParams const & ip = inset.params();
662 Buffer * child = loadIfNeeded(buf, ip);
665 parse_error |= !child->errorList("Parse").empty();
666 loadChildDocuments(*child);
669 if (use_gui && buf.getMasterBuffer() == &buf)