The following patch is in the lines of what I would need to gracefully catch buffer load parse errors (the last inseterror user).
The problem with the present code IMO is that BufferList::loadLyXFile [and BufferList::readFile] does too many tasks at once: it looks for existence of this buffer on the list, if it's not there then it creates the buffer and then tries to load the .lyx into that buffer. This patch tries to do the split, moving the responsibility of these decisions to the caller. It probably introduces some bugs, as it is a first trial and there's even code that I don't understand fully etc... so it's probably not finished, I need to reread the original code, do some testing etc. I want to know if I'm on the right track before going on. Thanks for any advice. Alfredo Index: BufferView.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView.C,v retrieving revision 1.160 diff -u -r1.160 BufferView.C --- BufferView.C 16 Jun 2003 11:49:27 -0000 1.160 +++ BufferView.C 19 Jun 2003 07:23:27 -0000 @@ -104,11 +104,17 @@ } +bool BufferView::loadLyXFile(string const & fn, bool tl) +{ + pimpl_->loadLyXFile(fn, tl); +} + + void BufferView::reload() { string const fn = buffer()->fileName(); if (bufferlist.close(buffer(), false)) - buffer(bufferlist.loadLyXFile(fn)); + loadLyXFile(fn); } Index: BufferView.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView.h,v retrieving revision 1.125 diff -u -r1.125 BufferView.h --- BufferView.h 23 May 2003 07:44:06 -0000 1.125 +++ BufferView.h 19 Jun 2003 07:23:27 -0000 @@ -81,6 +81,8 @@ /// reload the contained buffer void reload(); + /// load a buffer into the view + bool loadLyXFile(string const &, bool tolastfiles = true); /// fit the user cursor within the visible view bool fitCursor(); Index: BufferView_pimpl.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.C,v retrieving revision 1.376 diff -u -r1.376 BufferView_pimpl.C --- BufferView_pimpl.C 17 Jun 2003 15:33:45 -0000 1.376 +++ BufferView_pimpl.C 19 Jun 2003 07:23:29 -0000 @@ -28,6 +28,7 @@ #include "lyxtext.h" #include "lyxrc.h" #include "lyxrow.h" +#include "lastfiles.h" #include "paragraph.h" #include "ParagraphParameters.h" #include "TextCache.h" @@ -128,6 +129,62 @@ } + +bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles) +{ + // get absolute path of file and add ".lyx" to the filename if + // necessary + string s = FileSearch(string(), filename, "lyx"); + if (s.empty()) { + s = filename; + } + + // file already open? + if (bufferlist.exists(s)) { + string const file = MakeDisplayPath(s, 20); + string text = bformat(_("The document %1$s is already " + "loaded.\n\nDo you want to revert " + "to the saved version?"), file); + int const ret = Alert::prompt(_("Revert to saved document?"), + text, 0, 1, _("&Revert"), _("&Switch to document")); + + if (ret != 0) { + bufferlist.close(buffer_, false); + buffer(bufferlist.getBuffer(s)); + return true; + } else { + // FIXME: should be LFUN_REVERT + if (!bufferlist.close(bufferlist.getBuffer(s), false)) + return false; + // Fall through to new load. (Asger) + } + } + Buffer * b = bufferlist.newBuffer(s); + + //this is the point to attach to the error signal in the buffer + + if (!bufferlist.loadLyXFile(b, s)) { + string text = bformat(_("The document %1$s does " + "not yet exist.\n\n" + "Do you want to create " + "a new document?"), s); + int const ret = Alert::prompt(_("Create new document?"), + text, 0, 1, _("&Create"), _("Cancel")); + + if (ret == 0) { + bufferlist.close(buffer_, false); + buffer(bufferlist.newFile(s, string(), true)); + } + } + + buffer(b); + + if (tolastfiles) + lastfiles->newFile(b->fileName()); + + return true; +} + WorkArea & BufferView::Pimpl::workarea() const { return *workarea_.get(); @@ -286,8 +343,8 @@ mark_set = bv_->text->selection.mark(); the_locking_inset = bv_->theLockingInset(); buffer_->resizeInsets(bv_); - // I don't think the delete and new are necessary here we just could - // call only init! (Jug 20020419) + // I don't think the delete and new are necessary here we + // just could call only init! (Jug 20020419) delete bv_->text; bv_->text = new LyXText(bv_); bv_->text->init(bv_); @@ -641,10 +698,15 @@ beforeChange(bv_->text); if (fname != buffer_->fileName()) { - Buffer * b = bufferlist.exists(fname) ? - bufferlist.getBuffer(fname) : - bufferlist.loadLyXFile(fname); // don't ask, just load it - if (b != 0) buffer(b); + Buffer * b; + if (bufferlist.exists(fname)) + b = bufferlist.getBuffer(fname); + else { + b = bufferlist.newBuffer(fname); + bufferlist.loadLyXFile(b, fname); // don't ask, just load it + } + if (b != 0) + buffer(b); } ParIterator par = buffer_->getParFromID(saved_positions[i].par_id); Index: BufferView_pimpl.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/BufferView_pimpl.h,v retrieving revision 1.88 diff -u -r1.88 BufferView_pimpl.h --- BufferView_pimpl.h 20 May 2003 16:51:28 -0000 1.88 +++ BufferView_pimpl.h 19 Jun 2003 07:23:29 -0000 @@ -54,6 +54,9 @@ * Repaint pixmap. Used for when we've made a visible * change but don't need the full update() logic */ + /// + bool loadLyXFile(string const &, bool); + /// void repaint(); /// void workAreaResize(); Index: CutAndPaste.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/CutAndPaste.C,v retrieving revision 1.103 diff -u -r1.103 CutAndPaste.C --- CutAndPaste.C 17 Jun 2003 15:33:45 -0000 1.103 +++ CutAndPaste.C 19 Jun 2003 07:23:30 -0000 @@ -31,6 +31,8 @@ #include "support/lstrings.h" #include "support/limited_stack.h" +#include <algorithm> + using std::endl; using std::pair; using std::make_pair; @@ -58,8 +60,23 @@ CutStack::const_iterator end = cuts.end(); for (; cit != end; ++cit) { ParagraphList const & pars = cit->first; - string asciiPar(pars.front().asString(&buffer, false), 0, 25); - selList.push_back(asciiPar); + string frontpar = pars.front().asString(&buffer, false); + string shown; + int size = pars.size(); + if (size > 1 && frontpar.size() == 0) { + frontpar = boost::next(pars.begin())->asString(&buffer, false); + --size; + } + if (size == 1 && frontpar.size() < 25) + shown = frontpar; + else { + string backpar = pars.back().asString(&buffer, false); + int frontsize = std::min(int(frontpar.size()), 10); + int backstart = std::max(0, int(backpar.size()) - 10); + shown = frontpar.substr(0, frontsize) + "..." + + backpar.substr(backstart); + } + selList.push_back(shown); } return selList; Index: bufferlist.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/bufferlist.C,v retrieving revision 1.117 diff -u -r1.117 bufferlist.C --- bufferlist.C 10 Jun 2003 14:39:42 -0000 1.117 +++ bufferlist.C 19 Jun 2003 07:23:30 -0000 @@ -307,7 +307,7 @@ -Buffer * BufferList::readFile(string const & s, bool ronly) +bool BufferList::readFile(Buffer * b, string const & s, bool ronly) { string ts(s); string e = OnlyPath(s); @@ -320,11 +320,9 @@ string text = bformat(_("The specified document\n%1$s" "\ncould not be read."), file); Alert::error(_("Could not read document"), text); - return 0; + return false; } - Buffer * b = newBuffer(s, ronly); - // Check if emergency save file exists and is newer. e += OnlyFilename(s) + ".emergency"; FileInfo fileInfoE(e); @@ -381,10 +379,10 @@ LyXLex lex(0, 0); lex.setFile(ts); if (b->readFile(lex, ts)) - return b; + return true; else { release(b); - return 0; + return false; } } @@ -460,49 +458,19 @@ } -Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles) +bool BufferList::loadLyXFile(Buffer * b, string const & s) { - // get absolute path of file and add ".lyx" to the filename if - // necessary - string s = FileSearch(string(), filename, "lyx"); - if (s.empty()) { - s = filename; - } - - // file already open? - if (exists(s)) { - string const file = MakeDisplayPath(s, 20); - string text = bformat(_("The document %1$s is already loaded.\n\n" - "Do you want to revert to the saved version?"), file); - int const ret = Alert::prompt(_("Revert to saved document?"), - text, 0, 1, _("&Revert"), _("&Switch to document")); - - if (ret == 0) { - // FIXME: should be LFUN_REVERT - if (!close(getBuffer(s), false)) { - return 0; - } - // Fall through to new load. (Asger) - } else { - // Here, we pretend that we just loaded the - // open document - return getBuffer(s); - } - } - - Buffer * b = 0; bool ro = false; switch (IsFileWriteable(s)) { case 0: ro = true; // Fall through case 1: - b = readFile(s, ro); - if (b) { + if (readFile(b, s, ro)) { b->lyxvc.file_found_hook(s); } break; //fine- it's r/w - case -1: { + case -1: string const file = MakeDisplayPath(s, 20); // Here we probably should run if (LyXVC::file_not_found_hook(s)) { @@ -516,26 +484,14 @@ // With the current VC support it has to be, // a RCS file since CVS do not have special ,v files. RCS::retrieve(s); - return loadLyXFile(filename, tolastfiles); + return loadLyXFile(b, s); } } - string text = bformat(_("The document %1$s does not yet exist.\n\n" - "Do you want to create a new document?"), file); - int const ret = Alert::prompt(_("Create new document?"), - text, 0, 1, _("&Create"), _("Cancel")); - - if (ret == 0) - b = newFile(s, string(), true); - - break; - } + return false; } - if (b && tolastfiles) - lastfiles->newFile(b->fileName()); - - return b; + return true; } Index: bufferlist.h =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/bufferlist.h,v retrieving revision 1.39 diff -u -r1.39 bufferlist.h --- bufferlist.h 22 May 2003 18:58:55 -0000 1.39 +++ bufferlist.h 19 Jun 2003 07:23:31 -0000 @@ -33,12 +33,9 @@ Loads a LyX file or... \param filename The filename to read from. - \param tolastfiles Wether the file should be put in the - last opened files list or not. - \return The newly loaded LyX file. + \return success status. */ - Buffer * loadLyXFile(string const & filename, - bool tolastfiles = true); + bool loadLyXFile(Buffer *, string const & filename); /// write all buffers, asking the user, returns false if cancelled bool quitWriteAll(); @@ -52,11 +49,12 @@ /// Close all open buffers. void closeAll(); - /// read the given file - Buffer * readFile(string const &, bool ro); + /// read the given file into the given Buffer + bool readFile(Buffer *, string const &, bool ro); /// Make a new file (buffer) using a template Buffer * newFile(string const &, string, bool isNamed = false); + /// returns a vector with all the buffers filenames std::vector<string> const getFileNames() const; Index: importer.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/importer.C,v retrieving revision 1.27 diff -u -r1.27 importer.C --- importer.C 13 May 2003 09:48:42 -0000 1.27 +++ importer.C 19 Jun 2003 07:23:31 -0000 @@ -65,9 +65,7 @@ if (loader_format == "lyx") { - Buffer * buffer = bufferlist.loadLyXFile(lyxfile); - if (buffer) - lv->view()->buffer(buffer); + lv->view()->loadLyXFile(lyxfile); } else { lv->view()->buffer(bufferlist.newFile(lyxfile, string(), true)); bool as_paragraphs = loader_format == "textparagraph"; Index: lyx_main.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyx_main.C,v retrieving revision 1.148 diff -u -r1.148 lyx_main.C --- lyx_main.C 11 Jun 2003 19:21:46 -0000 1.148 +++ lyx_main.C 19 Jun 2003 07:23:32 -0000 @@ -142,7 +142,8 @@ vector<string>::iterator it = files.begin(); vector<string>::iterator end = files.end(); for (; it != end; ++it) { - last_loaded = bufferlist.loadLyXFile(*it); + last_loaded = bufferlist.newBuffer(*it, false); + bufferlist.loadLyXFile(last_loaded, *it); } files.clear(); Index: lyxfunc.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/lyxfunc.C,v retrieving revision 1.454 diff -u -r1.454 lyxfunc.C --- lyxfunc.C 17 Jun 2003 13:16:24 -0000 1.454 +++ lyxfunc.C 19 Jun 2003 07:23:34 -0000 @@ -1199,7 +1199,7 @@ } owner->message(bformat(_("Opening help file %1$s..."), MakeDisplayPath(fname))); - view()->buffer(bufferlist.loadLyXFile(fname, false)); + view()->loadLyXFile(fname, false); break; } @@ -1340,7 +1340,7 @@ if (bufferlist.exists(s)) { view()->buffer(bufferlist.getBuffer(s)); } else { - view()->buffer(bufferlist.loadLyXFile(s)); + view()->loadLyXFile(s); } view()->setCursorFromRow(row); @@ -1469,7 +1469,7 @@ if (bufferlist.exists(filename)) view()->buffer(bufferlist.getBuffer(filename)); else - view()->buffer(bufferlist.loadLyXFile(filename)); + view()->loadLyXFile(filename); } break; @@ -1798,10 +1798,8 @@ owner->message(bformat(_("Opening document %1$s..."), disp_fn)); - Buffer * openbuf = bufferlist.loadLyXFile(filename); string str2; - if (openbuf) { - view()->buffer(openbuf); + if (view()->loadLyXFile(filename)) { str2 = bformat(_("Document %1$s opened."), disp_fn); } else { str2 = bformat(_("Could not open document %1$s"), disp_fn); Index: frontends/qt2/lyx_gui.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/lyx_gui.C,v retrieving revision 1.40 diff -u -r1.40 lyx_gui.C --- frontends/qt2/lyx_gui.C 3 Jun 2003 19:32:07 -0000 1.40 +++ frontends/qt2/lyx_gui.C 19 Jun 2003 07:23:36 -0000 @@ -153,10 +153,9 @@ vector<string>::const_iterator cit = files.begin(); vector<string>::const_iterator end = files.end(); for (; cit != end; ++cit) { - Buffer * b = bufferlist.loadLyXFile(*cit); - if (b) { + Buffer * b = bufferlist.newBuffer(*cit); + if (bufferlist.loadLyXFile(b, *cit)) last = b; - } } // switch to the last buffer successfully loaded Index: insets/insetinclude.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/insets/insetinclude.C,v retrieving revision 1.124 diff -u -r1.124 insetinclude.C --- insets/insetinclude.C 16 Jun 2003 11:49:31 -0000 1.124 +++ insets/insetinclude.C 19 Jun 2003 07:23:38 -0000 @@ -293,8 +293,8 @@ FileInfo finfo(getFileName()); if (!finfo.isOK()) return false; - - return bufferlist.loadLyXFile(getFileName(), false) != 0; + return bufferlist.loadLyXFile(bufferlist.newBuffer(getFileName()), + getFileName()); }