}
+bool BufferView::loadLyXFile(string const & fn, bool tl)
+{
+ return pimpl_->loadLyXFile(fn, tl);
+}
+
+
void BufferView::reload()
{
string const fn = buffer()->fileName();
if (bufferlist.close(buffer(), false))
- buffer(bufferlist.loadLyXFile(fn));
+ loadLyXFile(fn);
}
/// reload the contained buffer
void reload();
+ /// load a buffer into the view
+ bool loadLyXFile(string const & name, bool tolastfiles = true);
/// fit the user cursor within the visible view
bool fitCursor();
#include "BufferView_pimpl.h"
#include "bufferlist.h"
#include "buffer.h"
+#include "buffer_funcs.h"
#include "bufferview_funcs.h"
#include "lfuns.h"
#include "debug.h"
#include "lyxtext.h"
#include "lyxrc.h"
#include "lyxrow.h"
+#include "lastfiles.h"
#include "paragraph.h"
#include "ParagraphParameters.h"
#include "TextCache.h"
}
+
+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) {
+ 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 (!::loadLyXFile(b, s)) {
+ bufferlist.release(b);
+ 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(newFile(s, string(), true));
+ }
+ }
+
+ buffer(b);
+
+ if (tolastfiles)
+ lastfiles->newFile(b->fileName());
+
+ return true;
+}
+
WorkArea & BufferView::Pimpl::workarea() const
{
return *workarea_.get();
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_);
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);
+ ::loadLyXFile(b, fname); // don't ask, just load it
+ }
+ if (b != 0)
+ buffer(b);
}
ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
* 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();
+
+2003-06-19 Alfredo Braunstein <abraunst@libero.it>
+
+ * bufferlist.[Ch] (loadLyXFile, readFile, newFile): removed the
+ ability to create a buffer and to return an existing one from
+ the list. Moved these functions to...
+ * buffer_funcs.[Ch]: added
+ * BufferView.[Ch] (loadLyXFile): added
+ * BufferView_pimpl.[Ch] (loadLyXFile): Added. Does the guessing
+ job removed from bufferlist::loadLyXFile.
+ * buffer.C (setReadOnly): make it work without view
+ (i.e added an if (users))
+
2003-06-19 Angus Leeming <leeming@lyx.org>
* lfuns.h:
broken_headers.h \
buffer.C \
buffer.h \
+ buffer_funcs.C \
+ buffer_funcs.h \
bufferlist.C \
bufferlist.h \
bufferparams.C \
if (read_only != flag) {
read_only = flag;
updateTitles();
- users->owner()->getDialogs().updateBufferDependent(false);
+ if (users)
+ users->owner()->getDialogs().updateBufferDependent(false);
}
}
--- /dev/null
+/**
+ * \file buffer_funcs.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ * \author Alfredo Braunstein
+ *
+ * Full author contact details are available in file CREDITS
+ *
+ */
+
+#include <config.h>
+
+#include "buffer_funcs.h"
+#include "bufferlist.h"
+#include "buffer.h"
+#include "gettext.h"
+#include "vc-backend.h"
+#include "lyxlex.h"
+#include "ParagraphList.h"
+#include "paragraph.h"
+
+#include "frontends/Alert.h"
+
+#include "support/filetools.h"
+#include "support/FileInfo.h"
+#include "support/lyxlib.h"
+
+
+extern BufferList bufferlist;
+
+
+namespace {
+
+bool readFile(Buffer * b, string const & s)
+{
+ string ts(s);
+ string e = OnlyPath(s);
+ string a = e;
+ // File information about normal file
+ FileInfo fileInfo(s);
+
+ if (!fileInfo.exist()) {
+ string const file = MakeDisplayPath(s, 50);
+ string text = bformat(_("The specified document\n%1$s"
+ "\ncould not be read."), file);
+ Alert::error(_("Could not read document"), text);
+ return false;
+ }
+
+ // Check if emergency save file exists and is newer.
+ e += OnlyFilename(s) + ".emergency";
+ FileInfo fileInfoE(e);
+
+ bool use_emergency = false;
+
+ if (fileInfoE.exist() && fileInfo.exist()) {
+ if (fileInfoE.getModificationTime()
+ > fileInfo.getModificationTime()) {
+ string const file = MakeDisplayPath(s, 20);
+ string text = bformat(_("An emergency save of the document %1$s exists.\n"
+ "\nRecover emergency save?"), file);
+ int const ret = Alert::prompt(_("Load emergency save?"),
+ text, 0, 1, _("&Recover"), _("&Load Original"));
+
+ if (ret == 0) {
+ ts = e;
+ // the file is not saved if we load the
+ // emergency file.
+ b->markDirty();
+ use_emergency = true;
+ }
+ }
+ }
+
+ if (!use_emergency) {
+ // Now check if autosave file is newer.
+ a += '#';
+ a += OnlyFilename(s);
+ a += '#';
+ FileInfo fileInfoA(a);
+ if (fileInfoA.exist() && fileInfo.exist()) {
+ if (fileInfoA.getModificationTime()
+ > fileInfo.getModificationTime()) {
+ string const file = MakeDisplayPath(s, 20);
+ string text = bformat(_("The backup of the document %1$s is newer.\n\n"
+ "Load the backup instead?"), file);
+ int const ret = Alert::prompt(_("Load backup?"),
+ text, 0, 1, _("&Load backup"), _("Load &original"));
+
+ if (ret == 0) {
+ ts = a;
+ // the file is not saved if we load the
+ // autosave file.
+ b->markDirty();
+ } else {
+ // Here, we should delete the autosave
+ lyx::unlink(a);
+ }
+ }
+ }
+ }
+ // not sure if this is the correct place to begin LyXLex
+ LyXLex lex(0, 0);
+ lex.setFile(ts);
+
+ return b->readFile(lex, ts);
+}
+
+
+} // namespace anon
+
+
+
+bool loadLyXFile(Buffer * b, string const & s)
+{
+ switch (IsFileWriteable(s)) {
+ case 0:
+ b->setReadonly(true);
+ // Fall through
+ case 1:
+ if (readFile(b, s)) {
+ b->lyxvc.file_found_hook(s);
+ return true;
+ }
+ break;
+ case -1:
+ string const file = MakeDisplayPath(s, 20);
+ // Here we probably should run
+ if (LyXVC::file_not_found_hook(s)) {
+ string text = bformat(_("Do you want to retrieve the document"
+ " %1$s from version control?"), file);
+ int const ret = Alert::prompt(_("Retrieve from version control?"),
+ text, 0, 1, _("&Retrieve"), _("&Cancel"));
+
+ if (ret == 0) {
+ // How can we know _how_ to do the checkout?
+ // 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(b, s);
+ }
+ }
+ break;
+ }
+ return false;
+}
+
+
+Buffer * newFile(string const & filename, string const & templatename,
+ bool isNamed)
+{
+ // get a free buffer
+ Buffer * b = bufferlist.newBuffer(filename);
+
+ string tname;
+ // use defaults.lyx as a default template if it exists.
+ if (templatename.empty())
+ tname = LibFileSearch("templates", "defaults.lyx");
+ else
+ tname = templatename;
+
+ if (!tname.empty()) {
+ bool templateok = false;
+ LyXLex lex(0, 0);
+ lex.setFile(tname);
+ if (lex.isOK()) {
+ if (b->readFile(lex, tname)) {
+ templateok = true;
+ }
+ }
+ if (!templateok) {
+ string const file = MakeDisplayPath(tname, 50);
+ string text = bformat(_("The specified document template\n%1$s\n"
+ "could not be read."), file);
+ Alert::error(_("Could not read template"), text);
+ // no template, start with empty buffer
+ b->paragraphs.push_back(Paragraph());
+ b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
+ }
+ } else { // start with empty buffer
+ b->paragraphs.push_back(Paragraph());
+ b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
+ }
+
+ if (!isNamed) {
+ b->setUnnamed();
+ b->setFileName(filename);
+ }
+
+ b->setReadonly(false);
+ b->updateDocLang(b->params.language);
+
+ return b;
+}
--- /dev/null
+// -*- C++ -*-
+/* \file buffer_funcs.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ * \author Alfredo Braunstein
+ *
+ * Full author contact details are available in file CREDITS
+ */
+
+#include "LString.h"
+
+class Buffer;
+
+/**
+ * Loads a LyX file \c filename into \c Buffer
+ * and \return success status.
+ */
+bool loadLyXFile(Buffer *, string const & filename);
+
+/* Make a new file (buffer) with name \c filename based on a template
+ * named \c templatename
+ */
+Buffer * newFile(string const & filename, string const & templatename,
+ bool isNamed = false);
}
-
-Buffer * BufferList::readFile(string const & s, bool ronly)
-{
- string ts(s);
- string e = OnlyPath(s);
- string a = e;
- // File information about normal file
- FileInfo fileInfo2(s);
-
- if (!fileInfo2.exist()) {
- string const file = MakeDisplayPath(s, 50);
- string text = bformat(_("The specified document\n%1$s"
- "\ncould not be read."), file);
- Alert::error(_("Could not read document"), text);
- return 0;
- }
-
- Buffer * b = newBuffer(s, ronly);
-
- // Check if emergency save file exists and is newer.
- e += OnlyFilename(s) + ".emergency";
- FileInfo fileInfoE(e);
-
- bool use_emergency = false;
-
- if (fileInfoE.exist() && fileInfo2.exist()) {
- if (fileInfoE.getModificationTime()
- > fileInfo2.getModificationTime()) {
- string const file = MakeDisplayPath(s, 20);
- string text = bformat(_("An emergency save of the document %1$s exists.\n"
- "\nRecover emergency save?"), file);
- int const ret = Alert::prompt(_("Load emergency save?"),
- text, 0, 1, _("&Recover"), _("&Load Original"));
-
- if (ret == 0) {
- ts = e;
- // the file is not saved if we load the
- // emergency file.
- b->markDirty();
- use_emergency = true;
- }
- }
- }
-
- if (!use_emergency) {
- // Now check if autosave file is newer.
- a += '#';
- a += OnlyFilename(s);
- a += '#';
- FileInfo fileInfoA(a);
- if (fileInfoA.exist() && fileInfo2.exist()) {
- if (fileInfoA.getModificationTime()
- > fileInfo2.getModificationTime()) {
- string const file = MakeDisplayPath(s, 20);
- string text = bformat(_("The backup of the document %1$s is newer.\n\n"
- "Load the backup instead?"), file);
- int const ret = Alert::prompt(_("Load backup?"),
- text, 0, 1, _("&Load backup"), _("Load &original"));
-
- if (ret == 0) {
- ts = a;
- // the file is not saved if we load the
- // autosave file.
- b->markDirty();
- } else {
- // Here, we should delete the autosave
- lyx::unlink(a);
- }
- }
- }
- }
- // not sure if this is the correct place to begin LyXLex
- LyXLex lex(0, 0);
- lex.setFile(ts);
- if (b->readFile(lex, ts))
- return b;
- else {
- release(b);
- return 0;
- }
-}
-
-
bool BufferList::exists(string const & s) const
{
return find_if(bstore.begin(), bstore.end(),
}
-Buffer * BufferList::newFile(string const & name, string tname, bool isNamed)
-{
- // get a free buffer
- Buffer * b = newBuffer(name);
-
- // use defaults.lyx as a default template if it exists.
- if (tname.empty()) {
- tname = LibFileSearch("templates", "defaults.lyx");
- }
- if (!tname.empty()) {
- bool templateok = false;
- LyXLex lex(0, 0);
- lex.setFile(tname);
- if (lex.isOK()) {
- if (b->readFile(lex, tname)) {
- templateok = true;
- }
- }
- if (!templateok) {
- string const file = MakeDisplayPath(tname, 50);
- string text = bformat(_("The specified document template\n%1$s\n"
- "could not be read."), file);
- Alert::error(_("Could not read template"), text);
- // no template, start with empty buffer
- b->paragraphs.push_back(Paragraph());
- b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
- }
- } else { // start with empty buffer
- b->paragraphs.push_back(Paragraph());
- b->paragraphs.begin()->layout(b->params.getLyXTextClass().defaultLayout());
- }
-
- if (!isNamed) {
- b->setUnnamed();
- b->setFileName(name);
- }
-
- b->setReadonly(false);
- b->updateDocLang(b->params.language);
-
- return b;
-}
-
-
-Buffer * BufferList::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 (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) {
- b->lyxvc.file_found_hook(s);
- }
- break; //fine- it's r/w
- case -1: {
- string const file = MakeDisplayPath(s, 20);
- // Here we probably should run
- if (LyXVC::file_not_found_hook(s)) {
- string text = bformat(_("Do you want to retrieve the document"
- " %1$s from version control?"), file);
- int const ret = Alert::prompt(_("Retrieve from version control?"),
- text, 0, 1, _("&Retrieve"), _("&Cancel"));
-
- if (ret == 0) {
- // How can we know _how_ to do the checkout?
- // 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);
- }
- }
-
- 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;
- }
- }
-
- if (b && tolastfiles)
- lastfiles->newFile(b->fileName());
-
- return b;
-}
-
-
void BufferList::setCurrentAuthor(string const & name, string const & email)
{
BufferStorage::iterator it = bstore.begin();
public:
BufferList();
- /**
- 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.
- */
- Buffer * loadLyXFile(string const & filename,
- bool tolastfiles = true);
-
/// write all buffers, asking the user, returns false if cancelled
bool quitWriteAll();
/// Close all open buffers.
void closeAll();
- /// read the given file
- Buffer * readFile(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;
+
+2003-06-19 Alfredo Braunstein <abraunst@libero.it>
+
+ * lyx_gui.C (start): call ::loadLyXFile instead
+ of BufferList::loadLyXFile
+
2003-06-19 Angus Leeming <leeming@lyx.org>
* Dialogs.C:
// FIXME: move this stuff out again
#include "bufferlist.h"
+#include "buffer_funcs.h"
#include "lyxfunc.h"
#include "lyxserver.h"
#include "BufferView.h"
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 (loadLyXFile(b, *cit))
last = b;
- }
}
// switch to the last buffer successfully loaded
+2003-06-19 Alfredo Braunstein <abraunst@libero.it>
+
+ * lyx_gui.C (start): call ::loadLyXFile instead
+ of BufferList::loadLyXFile
+
2003-06-19 Angus Leeming <leeming@lyx.org>
* Dialogs.C:
// FIXME: move this stuff out again
#include "bufferlist.h"
+#include "buffer_funcs.h"
#include "lyxfunc.h"
#include "lyxserver.h"
#include "BufferView.h"
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 (loadLyXFile(b, *cit))
last = b;
- }
}
// switch to the last buffer successfully loaded
#include "funcrequest.h"
#include "bufferlist.h"
+#include "buffer_funcs.h"
#include "support/filetools.h"
#include "frontends/Alert.h"
#include "gettext.h"
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));
+ lv->view()->buffer(newFile(lyxfile, string(), true));
bool as_paragraphs = loader_format == "textparagraph";
string filename2 = (loader_format == format) ? filename
: ChangeExtension(filename,
+2003-06-19 Alfredo Braunstein <abraunst@libero.it>
+
+ * insetinclude.C (loadIfNeeded): call ::loadLyXFile instead
+ of BufferList::loadLyXFile
+
2003-06-18 Lars Gullik Bjønnes <larsbj@gullik.net>
* insettext.C (update): simplify
#include "insetinclude.h"
#include "buffer.h"
+#include "buffer_funcs.h"
#include "bufferlist.h"
#include "BufferView.h"
#include "debug.h"
FileInfo finfo(getFileName());
if (!finfo.isOK())
return false;
-
- return bufferlist.loadLyXFile(getFileName(), false) != 0;
+ return loadLyXFile(bufferlist.newBuffer(getFileName()),
+ getFileName());
}
#include "lyx_cb.h"
#include "lyx_main.h"
#include "buffer.h"
+#include "buffer_funcs.h"
#include "bufferlist.h"
#include "bufferview_funcs.h"
#include "debug.h"
<< "\nTemplate is " << tmpname << endl;
// find a free buffer
- Buffer * tmpbuf = bufferlist.newFile(name, tmpname);
+ Buffer * tmpbuf = newFile(name, tmpname);
if (tmpbuf)
lastfiles->newFile(tmpbuf->fileName());
return tmpbuf;
#include "bufferlist.h"
#include "buffer.h"
+#include "buffer_funcs.h"
#include "lyxserver.h"
#include "kbmap.h"
#include "lyxfunc.h"
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);
+ loadLyXFile(last_loaded, *it);
}
files.clear();
// no buffer loaded, create one
+ string const tmpfile = "tmpfile";
if (!last_loaded)
- last_loaded = bufferlist.newFile("tmpfile", string());
+ last_loaded = newFile(tmpfile, string());
bool success = false;
#include "lyxrow.h"
#include "bufferlist.h"
#include "buffer.h"
+#include "buffer_funcs.h"
#include "BufferView.h"
#include "lyxserver.h"
#include "intl.h"
}
owner->message(bformat(_("Opening help file %1$s..."),
MakeDisplayPath(fname)));
- view()->buffer(bufferlist.loadLyXFile(fname, false));
+ view()->loadLyXFile(fname, false);
break;
}
if (bufferlist.exists(s)) {
view()->buffer(bufferlist.getBuffer(s));
} else {
- view()->buffer(bufferlist.loadLyXFile(s));
+ view()->loadLyXFile(s);
}
view()->setCursorFromRow(row);
if (bufferlist.exists(filename))
view()->buffer(bufferlist.getBuffer(filename));
else
- view()->buffer(bufferlist.loadLyXFile(filename));
+ view()->loadLyXFile(filename);
}
break;
templname = fname;
}
- view()->buffer(bufferlist.newFile(filename, templname, !name.empty()));
+ view()->buffer(newFile(filename, templname, !name.empty()));
}
FileInfo const f(filename, true);
if (!f.exist()) {
// the user specifically chose this name. Believe them.
- Buffer * buffer = bufferlist.newFile(filename, "", true);
+ Buffer * buffer = newFile(filename, "", true);
view()->buffer(buffer);
return;
}
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);