]> git.lyx.org Git - lyx.git/blobdiff - src/callback.cpp
Fulfill promise to Andre: TextClass_ptr --> TextClassPtr.
[lyx.git] / src / callback.cpp
index 6eab414618c425ca1450681bc8d996fac911d3dd..2a41494be6f48791f17427a676d0799f278fc0ef 100644 (file)
@@ -29,8 +29,7 @@
 #include "LyX.h"
 #include "Layout.h"
 #include "LyXRC.h"
-#include "LyXText.h"
-#include "Paragraph.h"
+#include "Text.h"
 #include "Undo.h"
 
 #include "frontends/alert.h"
 #include <cerrno>
 #include <fstream>
 
+using std::back_inserter;
+using std::copy;
+using std::endl;
+using std::make_pair;
+using std::string;
+using std::ifstream;
+using std::ios;
+using std::istream_iterator;
+
+using boost::shared_ptr;
+namespace fs = boost::filesystem;
 
 namespace lyx {
 
@@ -77,21 +87,9 @@ using support::split;
 using support::Systemcall;
 using support::tempName;
 using support::unlink;
-
-using boost::shared_ptr;
+using frontend::LyXView;
 
 namespace Alert = frontend::Alert;
-namespace fs = boost::filesystem;
-
-using std::back_inserter;
-using std::copy;
-using std::endl;
-using std::make_pair;
-using std::string;
-using std::ifstream;
-using std::ios;
-using std::istream_iterator;
-
 
 // this should be static, but I need it in Buffer.cpp
 bool quitting; // flag, that we are quitting the program
@@ -112,7 +110,8 @@ bool menuWrite(Buffer * buffer)
        docstring const file = makeDisplayPath(buffer->fileName(), 30);
 
        docstring text = bformat(_("The document %1$s could not be saved.\n\n"
-                                            "Do you want to rename the document and try again?"), file);
+                                  "Do you want to rename the document and "
+                                  "try again?"), file);
        int const ret = Alert::prompt(_("Rename and save?"),
                text, 0, 1, _("&Rename"), _("&Cancel"));
 
@@ -123,18 +122,34 @@ bool menuWrite(Buffer * buffer)
 
 
 
+/** Write a buffer to a new file name and rename the buffer
+    according to the new file name.
+
+    This function is e.g. used by menu callbacks and
+    LFUN_BUFFER_WRITE_AS.
+
+    If 'newname' is empty (the default), the user is asked via a
+    dialog for the buffer's new name and location.
+
+    If 'newname' is non-empty and has an absolute path, that is used.
+    Otherwise the base directory of the buffer is used as the base
+    for any relative path in 'newname'.
+*/
+
 bool writeAs(Buffer * buffer, string const & newname)
 {
        string fname = buffer->fileName();
        string const oldname = fname;
 
-       if (newname.empty()) {
+       if (newname.empty()) {  /// No argument? Ask user through dialog
 
                // FIXME UNICODE
                FileDialog fileDlg(_("Choose a filename to save document as"),
-                       LFUN_BUFFER_WRITE_AS,
-                       make_pair(_("Documents|#o#O"), from_utf8(lyxrc.document_path)),
-                       make_pair(_("Templates|#T#t"), from_utf8(lyxrc.template_path)));
+                                  LFUN_BUFFER_WRITE_AS,
+                                  make_pair(_("Documents|#o#O"), 
+                                            from_utf8(lyxrc.document_path)),
+                                  make_pair(_("Templates|#T#t"), 
+                                            from_utf8(lyxrc.template_path)));
 
                if (!isLyXFilename(fname))
                        fname += ".lyx";
@@ -158,16 +173,18 @@ bool writeAs(Buffer * buffer, string const & newname)
                fname = makeAbsPath(fname).absFilename();
                if (!isLyXFilename(fname))
                        fname += ".lyx";
-       } else
-               fname = newname;
 
-       FileName const filename(fname);
-       if (fs::exists(filename.toFilesystemEncoding())) {
+       } else 
+               fname = makeAbsPath(newname, onlyPath(oldname)).absFilename();
+
+       if (fs::exists(FileName(fname).toFilesystemEncoding())) {
                docstring const file = makeDisplayPath(fname, 30);
-               docstring text = bformat(_("The document %1$s already exists.\n\n"
-                                                    "Do you want to over-write that document?"), file);
-               int const ret = Alert::prompt(_("Over-write document?"),
-                       text, 0, 1, _("&Over-write"), _("&Cancel"));
+               docstring text = bformat(_("The document %1$s already "
+                                          "exists.\n\nDo you want to "
+                                          "overwrite that document?"), 
+                                        file);
+               int const ret = Alert::prompt(_("Overwrite document?"),
+                       text, 0, 1, _("&Overwrite"), _("&Cancel"));
 
                if (ret == 1)
                        return false;
@@ -178,10 +195,12 @@ bool writeAs(Buffer * buffer, string const & newname)
        buffer->markDirty();
        bool unnamed = buffer->isUnnamed();
        buffer->setUnnamed(false);
+       buffer->saveCheckSum(fname);
 
        if (!menuWrite(buffer)) {
                buffer->setFileName(oldname);
                buffer->setUnnamed(unnamed);
+               buffer->saveCheckSum(oldname);
                return false;
        }
 
@@ -215,7 +234,8 @@ private:
 
 int AutoSaveBuffer::start()
 {
-       command_ = to_utf8(bformat(_("Auto-saving %1$s"), from_utf8(fname_.absFilename())));
+       command_ = to_utf8(bformat(_("Auto-saving %1$s"), 
+                                  from_utf8(fname_.absFilename())));
        return run(DontWait);
 }
 
@@ -235,13 +255,14 @@ int AutoSaveBuffer::generateChild()
 
                FileName const tmp_ret(tempName(FileName(), "lyxauto"));
                if (!tmp_ret.empty()) {
-                       bv_.buffer()->writeFile(tmp_ret);
+                       bv_.buffer().writeFile(tmp_ret);
                        // assume successful write of tmp_ret
                        if (!rename(tmp_ret, fname_)) {
                                failed = true;
-                               // most likely couldn't move between filesystems
-                               // unless write of tmp_ret failed
-                               // so remove tmp file (if it exists)
+                               // most likely couldn't move between
+                               // filesystems unless write of tmp_ret
+                               // failed so remove tmp file (if it
+                               // exists)
                                unlink(tmp_ret);
                        }
                } else {
@@ -250,12 +271,11 @@ int AutoSaveBuffer::generateChild()
 
                if (failed) {
                        // failed to write/rename tmp_ret so try writing direct
-                       if (!bv_.buffer()->writeFile(fname_)) {
+                       if (!bv_.buffer().writeFile(fname_)) {
                                // It is dangerous to do this in the child,
                                // but safe in the parent, so...
-                               if (pid == -1)
-                                       // emit message signal.
-                                       bv_.buffer()->message(_("Autosave failed!"));
+                               if (pid == -1) // emit message signal.
+                                       bv_.buffer().message(_("Autosave failed!"));
                        }
                }
                if (pid == 0) { // we are the child so...
@@ -272,29 +292,26 @@ void autoSave(BufferView * bv)
        // should probably be moved into BufferList (Lgb)
        // Perfect target for a thread...
 {
-       if (!bv->buffer())
-               return;
-
-       if (bv->buffer()->isBakClean() || bv->buffer()->isReadonly()) {
+       if (bv->buffer().isBakClean() || bv->buffer().isReadonly()) {
                // We don't save now, but we'll try again later
-               bv->buffer()->resetAutosaveTimers();
+               bv->buffer().resetAutosaveTimers();
                return;
        }
 
        // emit message signal.
-       bv->buffer()->message(_("Autosaving current document..."));
+       bv->buffer().message(_("Autosaving current document..."));
 
        // create autosave filename
-       string fname = bv->buffer()->filePath();
+       string fname = bv->buffer().filePath();
        fname += '#';
-       fname += onlyFilename(bv->buffer()->fileName());
+       fname += onlyFilename(bv->buffer().fileName());
        fname += '#';
 
        AutoSaveBuffer autosave(*bv, FileName(fname));
        autosave.start();
 
-       bv->buffer()->markBakClean();
-       bv->buffer()->resetAutosaveTimers();
+       bv->buffer().markBakClean();
+       bv->buffer().resetAutosaveTimers();
 }
 
 
@@ -305,7 +322,7 @@ void autoSave(BufferView * bv)
 // create new file with template
 // SERVERCMD !
 //
-void newFile(BufferView * bv, string const & filename)
+void newFile(LyXView & lv, string const & filename)
 {
        // Split argument by :
        string name;
@@ -316,17 +333,16 @@ void newFile(BufferView * bv, string const & filename)
 
        Buffer * const b = newFile(name, tmpname);
        if (b)
-               bv->setBuffer(b);
+               lv.setBuffer(b);
 }
 
 
 // Insert plain text file (if filename is empty, prompt for one)
 void insertPlaintextFile(BufferView * bv, string const & f, bool asParagraph)
 {
-       if (!bv->buffer())
-               return;
+       docstring const tmpstr =
+         getContentsOfPlaintextFile(bv, f, asParagraph);
 
-       docstring const tmpstr = getContentsOfPlaintextFile(bv, f, asParagraph);
        if (tmpstr.empty())
                return;
 
@@ -341,16 +357,18 @@ void insertPlaintextFile(BufferView * bv, string const & f, bool asParagraph)
 
 
 docstring const getContentsOfPlaintextFile(BufferView * bv, string const & f,
-               bool asParagraph)
+                                          bool asParagraph)
 {
        FileName fname(f);
 
        if (fname.empty()) {
                FileDialog fileDlg(_("Select file to insert"),
-                       (asParagraph) ? LFUN_FILE_INSERT_PLAINTEXT_PARA : LFUN_FILE_INSERT_PLAINTEXT);
+                                  ( (asParagraph)
+                                    ? LFUN_FILE_INSERT_PLAINTEXT_PARA 
+                                    : LFUN_FILE_INSERT_PLAINTEXT) );
 
                FileDialog::Result result =
-                       fileDlg.open(from_utf8(bv->buffer()->filePath()),
+                       fileDlg.open(from_utf8(bv->buffer().filePath()),
                                     FileFilterList(), docstring());
 
                if (result.first == FileDialog::Later)
@@ -365,8 +383,9 @@ docstring const getContentsOfPlaintextFile(BufferView * bv, string const & f,
        if (!fs::is_readable(fname.toFilesystemEncoding())) {
                docstring const error = from_ascii(strerror(errno));
                docstring const file = makeDisplayPath(fname.absFilename(), 50);
-               docstring const text = bformat(_("Could not read the specified document\n"
-                                                          "%1$s\ndue to the error: %2$s"), file, error);
+               docstring const text =
+                 bformat(_("Could not read the specified document\n"
+                           "%1$s\ndue to the error: %2$s"), file, error);
                Alert::error(_("Could not read file"), text);
                return docstring();
        }
@@ -375,8 +394,9 @@ docstring const getContentsOfPlaintextFile(BufferView * bv, string const & f,
        if (!ifs) {
                docstring const error = from_ascii(strerror(errno));
                docstring const file = makeDisplayPath(fname.absFilename(), 50);
-               docstring const text = bformat(_("Could not open the specified document\n"
-                                                          "%1$s\ndue to the error: %2$s"), file, error);
+               docstring const text =
+                 bformat(_("Could not open the specified document\n"
+                           "%1$s\ndue to the error: %2$s"), file, error);
                Alert::error(_("Could not open file"), text);
                return docstring();
        }
@@ -399,7 +419,18 @@ docstring const getContentsOfPlaintextFile(BufferView * bv, string const & f,
 #endif
 
        // FIXME UNICODE: We don't know the encoding of the file
-       return normalize_kc(from_utf8(tmpstr));
+       docstring file_content = from_utf8(tmpstr);
+       if (file_content.empty()) {
+               Alert::error(_("Reading not UTF-8 encoded file"),
+                            _("The file is not UTF-8 encoded.\n"
+                              "It will be read as local 8Bit-encoded.\n"
+                              "If this does not give the correct result\n"
+                              "then please change the encoding of the file\n"
+                              "to UTF-8 with a program other than LyX.\n"));
+               file_content = from_local8bit(tmpstr);
+       }
+
+       return normalize_c(file_content);
 }
 
 
@@ -424,8 +455,8 @@ void reconfigure(LyXView & lv)
 
        Alert::information(_("System reconfigured"),
                           _("The system has been reconfigured.\n"
-                                         "You need to restart LyX to make use of any\n"
-                                         "updated document class specifications."));
+                            "You need to restart LyX to make use of any\n"
+                            "updated document class specifications."));
 }