]> git.lyx.org Git - lyx.git/blob - src/buffer_funcs.cpp
adjust
[lyx.git] / src / buffer_funcs.cpp
1 /**
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.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Alfredo Braunstein
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  */
12
13 #include <config.h>
14
15 #include "buffer_funcs.h"
16 #include "Buffer.h"
17 #include "BufferList.h"
18 #include "BufferParams.h"
19 #include "debug.h"
20 #include "DocIterator.h"
21 #include "Counters.h"
22 #include "ErrorList.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "gettext.h"
26 #include "InsetIterator.h"
27 #include "Language.h"
28 #include "LaTeX.h"
29 #include "Layout.h"
30 #include "LyX.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"
39 #include "LyXVC.h"
40 #include "TexRow.h"
41 #include "Text.h"
42 #include "TocBackend.h"
43 #include "VCBackend.h"
44
45 #include "frontends/alert.h"
46
47 #include "insets/InsetBibitem.h"
48 #include "insets/InsetInclude.h"
49
50 #include "support/filetools.h"
51 #include "support/fs_extras.h"
52 #include "support/lyxlib.h"
53
54 #include <boost/bind.hpp>
55 #include <boost/filesystem/operations.hpp>
56
57 using std::min;
58 using std::string;
59
60
61 namespace lyx {
62
63 using namespace std;
64
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;
73
74 namespace Alert = frontend::Alert;
75 namespace fs = boost::filesystem;
76
77 namespace {
78
79 bool readFile(Buffer * const b, FileName const & s)
80 {
81         BOOST_ASSERT(b);
82
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);
89                 return false;
90         }
91
92         // Check if emergency save file exists and is newer.
93         FileName const e(s.absFilename() + ".emergency");
94
95         if (fs::exists(e.toFilesystemEncoding()) &&
96             fs::exists(s.toFilesystemEncoding()) &&
97             fs::last_write_time(e.toFilesystemEncoding()) > fs::last_write_time(s.toFilesystemEncoding()))
98         {
99                 docstring const file = makeDisplayPath(s.absFilename(), 20);
100                 docstring const text =
101                         bformat(_("An emergency save of the document "
102                                   "%1$s exists.\n\n"
103                                                "Recover emergency save?"), file);
104                 switch (Alert::prompt(_("Load emergency save?"), text, 0, 2,
105                                       _("&Recover"),  _("&Load Original"),
106                                       _("&Cancel")))
107                 {
108                 case 0:
109                         // the file is not saved if we load the emergency file.
110                         b->markDirty();
111                         return b->readFile(e);
112                 case 1:
113                         break;
114                 default:
115                         return false;
116                 }
117         }
118
119         // Now check if autosave file is newer.
120         FileName const a(onlyPath(s.absFilename()) + '#' + onlyFilename(s.absFilename()) + '#');
121
122         if (fs::exists(a.toFilesystemEncoding()) &&
123             fs::exists(s.toFilesystemEncoding()) &&
124             fs::last_write_time(a.toFilesystemEncoding()) > fs::last_write_time(s.toFilesystemEncoding()))
125         {
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"),
133                                       _("&Cancel") ))
134                 {
135                 case 0:
136                         // the file is not saved if we load the autosave file.
137                         b->markDirty();
138                         return b->readFile(a);
139                 case 1:
140                         // Here we delete the autosave
141                         unlink(a);
142                         break;
143                 default:
144                         return false;
145                 }
146         }
147         return b->readFile(s);
148 }
149
150
151 } // namespace anon
152
153
154
155 bool loadLyXFile(Buffer * b, FileName const & s)
156 {
157         BOOST_ASSERT(b);
158
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);
164                         return true;
165                 }
166         } else {
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"));
175
176                         if (ret == 0) {
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.
180                                 RCS::retrieve(s);
181                                 return loadLyXFile(b, s);
182                         }
183                 }
184         }
185         return false;
186 }
187
188
189 bool checkIfLoaded(FileName const & fn)
190 {
191         return theBufferList().getBuffer(fn.absFilename());
192 }
193
194
195 Buffer * checkAndLoadLyXFile(FileName const & filename)
196 {
197         // File already open?
198         Buffer * checkBuffer = theBufferList().getBuffer(filename.absFilename());
199         if (checkBuffer) {
200                 if (checkBuffer->isClean())
201                         return checkBuffer;
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")))
208                         return checkBuffer;
209
210                 // FIXME: should be LFUN_REVERT
211                 if (theBufferList().close(checkBuffer, false))
212                         // Load it again.
213                         return checkAndLoadLyXFile(filename);
214                 else
215                         // The file could not be closed.
216                         return 0;
217         }
218
219         if (isFileReadable(filename)) {
220                 Buffer * b = theBufferList().newBuffer(filename.absFilename());
221                 if (!lyx::loadLyXFile(b, filename)) {
222                         theBufferList().release(b);
223                         return 0;
224                 }
225                 return b;
226         }
227
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);
234
235         return 0;
236 }
237
238 // FIXME newFile() should probably be a member method of Application...
239 Buffer * newFile(string const & filename, string const & templatename,
240                  bool const isNamed)
241 {
242         // get a free buffer
243         Buffer * b = theBufferList().newBuffer(filename);
244         BOOST_ASSERT(b);
245
246         FileName tname;
247         // use defaults.lyx as a default template if it exists.
248         if (templatename.empty())
249                 tname = libFileSearch("templates", "defaults.lyx");
250         else
251                 tname = makeAbsPath(templatename);
252
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."),
258                                 file);
259                         Alert::error(_("Could not read template"), text);
260                         theBufferList().release(b);
261                         return 0;
262                 }
263         }
264
265         if (!isNamed) {
266                 b->setUnnamed();
267                 b->setFileName(filename);
268         }
269
270         b->setReadonly(false);
271         b->fully_loaded(true);
272
273         return b;
274 }
275
276
277 void bufferErrors(Buffer const & buf, TeXErrors const & terr,
278                                   ErrorList & errorList)
279 {
280         TeXErrors::Errors::const_iterator cit = terr.begin();
281         TeXErrors::Errors::const_iterator end = terr.end();
282
283         for (; cit != end; ++cit) {
284                 int id_start = -1;
285                 int pos_start = -1;
286                 int errorrow = cit->error_in_line;
287                 bool found = buf.texrow().getIdFromRow(errorrow, id_start,
288                                                        pos_start);
289                 int id_end = -1;
290                 int pos_end = -1;
291                 do {
292                         ++errorrow;
293                         found = buf.texrow().getIdFromRow(errorrow, id_end,
294                                                           pos_end);
295                 } while (found && id_start == id_end && pos_start == pos_end);
296
297                 errorList.push_back(ErrorItem(cit->error_desc,
298                         cit->error_text, id_start, pos_start, pos_end));
299         }
300 }
301
302
303 string const bufferFormat(Buffer const & buffer)
304 {
305         if (buffer.isDocBook())
306                 return "docbook";
307         else if (buffer.isLiterate())
308                 return "literate";
309         else
310                 return "latex";
311 }
312
313
314 int countWords(DocIterator const & from, DocIterator const & to)
315 {
316         int count = 0;
317         bool inword = false;
318         for (DocIterator dit = from ; dit != to ; dit.forwardPos()) {
319                 // Copied and adapted from isLetter() in ControlSpellChecker
320                 if (dit.inTexted()
321                     && dit.pos() != dit.lastpos()
322                     && dit.paragraph().isLetter(dit.pos())
323                     && !dit.paragraph().isDeleted(dit.pos())) {
324                         if (!inword) {
325                                 ++count;
326                                 inword = true;
327                         }
328                 } else if (inword)
329                         inword = false;
330         }
331
332         return count;
333 }
334
335
336 namespace {
337
338 depth_type getDepth(DocIterator const & it)
339 {
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
345         return depth - 1;
346 }
347
348 depth_type getItemDepth(ParIterator const & it)
349 {
350         Paragraph const & par = *it;
351         LabelType const labeltype = par.layout()->labeltype;
352
353         if (labeltype != LABEL_ENUMERATE && labeltype != LABEL_ITEMIZE)
354                 return 0;
355
356         // this will hold the lowest depth encountered up to now.
357         depth_type min_depth = getDepth(it);
358         ParIterator prev_it = it;
359         while (true) {
360                 if (prev_it.pit())
361                         --prev_it.top().pit();
362                 else {
363                         // start of nested inset: go to outer par
364                         prev_it.pop_back();
365                         if (prev_it.empty()) {
366                                 // start of document: nothing to do
367                                 return 0;
368                         }
369                 }
370
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;
378                         }
379                         else if (prev_depth == min_depth) {
380                                 return prev_par.itemdepth;
381                         }
382                 }
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) {
387                         return 0;
388                 }
389         }
390 }
391
392
393 bool needEnumCounterReset(ParIterator const & it)
394 {
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;
404         }
405         // start of nested inset: reset
406         return true;
407 }
408
409
410 // set the label of a paragraph. This includes the counters.
411 void setLabel(Buffer const & buf, ParIterator & it)
412 {
413         TextClass const & textclass = buf.params().getTextClass();
414         Paragraph & par = it.paragraph();
415         LayoutPtr const & layout = par.layout();
416         Counters & counters = textclass.counters();
417
418         if (par.params().startOfAppendix()) {
419                 // FIXME: only the counter corresponding to toplevel
420                 // sectionning should be reset
421                 counters.reset();
422                 counters.appendix(true);
423         }
424         par.params().appendix(counters.appendix());
425
426         // Compute the item depth of the paragraph
427         par.itemdepth = getItemDepth(it);
428
429         if (layout->margintype == MARGIN_MANUAL) {
430                 if (par.params().labelWidthString().empty())
431                         par.params().labelWidthString(par.translateIfPossible(layout->labelstring(), buf.params()));
432         } else {
433                 par.params().labelWidthString(docstring());
434         }
435
436         switch(layout->labeltype) {
437         case LABEL_COUNTER:
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()));
444                 } else
445                         par.params().labelString(docstring());
446                 break;
447
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
454                 docstring itemlabel;
455                 switch (par.itemdepth) {
456                 case 0:
457                         itemlabel = char_type(0x2022);
458                         break;
459                 case 1:
460                         itemlabel = char_type(0x2013);
461                         break;
462                 case 2:
463                         itemlabel = char_type(0x2217);
464                         break;
465                 case 3:
466                         itemlabel = char_type(0x2219); // or 0x00b7
467                         break;
468                 }
469                 par.params().labelString(itemlabel);
470                 break;
471         }
472
473         case LABEL_ENUMERATE: {
474                 // FIXME: Yes I know this is a really, really! bad solution
475                 // (Lgb)
476                 docstring enumcounter = from_ascii("enum");
477
478                 switch (par.itemdepth) {
479                 case 2:
480                         enumcounter += 'i';
481                 case 1:
482                         enumcounter += 'i';
483                 case 0:
484                         enumcounter += 'i';
485                         break;
486                 case 3:
487                         enumcounter += "iv";
488                         break;
489                 default:
490                         // not a valid enumdepth...
491                         break;
492                 }
493
494                 // Maybe we have to reset the enumeration counter.
495                 if (needEnumCounterReset(it))
496                         counters.reset(enumcounter);
497
498                 counters.step(enumcounter);
499
500                 string format;
501
502                 switch (par.itemdepth) {
503                 case 0:
504                         format = N_("\\arabic{enumi}.");
505                         break;
506                 case 1:
507                         format = N_("(\\alph{enumii})");
508                         break;
509                 case 2:
510                         format = N_("\\roman{enumiii}.");
511                         break;
512                 case 3:
513                         format = N_("\\Alph{enumiv}.");
514                         break;
515                 default:
516                         // not a valid enumdepth...
517                         break;
518                 }
519
520                 par.params().labelString(counters.counterLabel(
521                         par.translateIfPossible(from_ascii(format), buf.params())));
522
523                 break;
524         }
525
526         case LABEL_SENSITIVE: {
527                 string const & type = counters.current_float();
528                 docstring full_label;
529                 if (type.empty())
530                         full_label = buf.B_("Senseless!!! ");
531                 else {
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:"), 
536                                                      name, 
537                                                      counters.theCounter(from_utf8(type)));
538                         } else
539                                 full_label = bformat(from_ascii("%1$s #:"), name);      
540                 }
541                 par.params().labelString(full_label);   
542                 break;
543         }
544
545         case LABEL_NO_LABEL:
546                 par.params().labelString(docstring());
547                 break;
548
549         case LABEL_MANUAL:
550         case LABEL_TOP_ENVIRONMENT:
551         case LABEL_CENTERED_TOP_ENVIRONMENT:
552         case LABEL_STATIC:      
553         case LABEL_BIBLIO:
554                 par.params().labelString(
555                         par.translateIfPossible(layout->labelstring(), 
556                                                 buf.params()));
557                 break;
558         }
559 }
560
561 } // anon namespace
562
563 void updateLabels(Buffer const & buf, ParIterator & parit)
564 {
565         BOOST_ASSERT(parit.pit() == 0);
566
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();
573
574                 // set the counter for this paragraph
575                 setLabel(buf, parit);
576
577                 // Now the insets
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);
583                 }
584         }
585         
586 }
587
588
589 // FIXME: buf should should be const because updateLabels() modifies
590 // the contents of the paragraphs.
591 void updateLabels(Buffer const & buf, bool childonly)
592 {
593         Buffer const * const master = buf.getMasterBuffer();
594         // Use the master text class also for child documents
595         TextClass const & textclass = master->params().getTextClass();
596
597         if (!childonly) {
598                 // If this is a child document start with the master
599                 if (master != &buf) {
600                         updateLabels(*master);
601                         return;
602                 }
603
604                 // start over the counters
605                 textclass.counters().reset();
606         }
607
608         Buffer & cbuf = const_cast<Buffer &>(buf);
609
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();
616                 return;
617         }
618
619         // do the real work
620         ParIterator parit = par_iterator_begin(buf.inset());
621         updateLabels(buf, parit);
622
623         cbuf.tocBackend().update();
624         if (!childonly)
625                 cbuf.structureChanged();
626         // FIXME
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();
631 }
632
633
634 void checkBufferStructure(Buffer & buffer, ParIterator const & par_it)
635 {
636         if (par_it->layout()->toclevel != Layout::NOT_IN_TOC) {
637                 Buffer * master = buffer.getMasterBuffer();
638                 master->tocBackend().updateItem(par_it);
639                 master->structureChanged();
640         }
641 }
642
643 textclass_type defaultTextclass()
644 {
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
648         // is what we want.
649         return textclasslist.numberOfClass("article").second;
650 }
651
652
653 void loadChildDocuments(Buffer const & buf)
654 {
655         bool parse_error = false;
656                 
657         for (InsetIterator it = inset_iterator_begin(buf.inset()); it; ++it) {
658                 if (it->lyxCode() != INCLUDE_CODE)
659                         continue;
660                 InsetInclude const & inset = static_cast<InsetInclude const &>(*it);
661                 InsetCommandParams const & ip = inset.params();
662                 Buffer * child = loadIfNeeded(buf, ip);
663                 if (!child)
664                         continue;
665                 parse_error |= !child->errorList("Parse").empty();
666                 loadChildDocuments(*child);
667         }
668
669         if (use_gui && buf.getMasterBuffer() == &buf)
670                 updateLabels(buf);
671 }
672 } // namespace lyx