]> git.lyx.org Git - features.git/commitdiff
implement undo for textclass switches in order to avoid a crash
authorAndré Pönitz <poenitz@gmx.net>
Thu, 14 Jul 2005 22:09:22 +0000 (22:09 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Thu, 14 Jul 2005 22:09:22 +0000 (22:09 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10190 a592a061-630c-0410-9148-cb99ea01b6c8

src/ChangeLog
src/buffer.C
src/lyxfind.C
src/lyxfunc.C
src/support/filetools.C
src/support/lstrings.C
src/undo.C
src/undo.h

index bf9964534674c604d471918ab465566caa5456d4..eee34cf82ebb343fa1fdf532eb98258d1fb295e3 100644 (file)
@@ -1,8 +1,13 @@
 
+2005-05-07  André Pönitz  <poenitz@gmx.net>
+
+       * undo.C (recordUndoFullBuffer): implement undo for
+  textclass switches
+
 2005-05-07  André Pönitz  <poenitz@gmx.net>
 
        * cursor.C (setSelection): open insets when selection is set there
-    to avoid crashs with cold coord cache
+  to avoid crashs with cold coord cache
 
 2005-05-07  André Pönitz  <poenitz@gmx.net>
 
index 808dc4b013063440ed688de5cfdb79eafbccc9e8..d6c34394d0fac1fa1c032f6b4bc41a2cfd2a9228 100644 (file)
@@ -213,8 +213,8 @@ Buffer::Impl::Impl(Buffer & parent, string const & file, bool readonly_)
 }
 
 
-Buffer::Buffer(string const & file, bool ronly)
-       : pimpl_(new Impl(*this, file, ronly))
+Buffer::Buffer(string const & file, bool readonly)
+       : pimpl_(new Impl(*this, file, readonly))
 {
        lyxerr[Debug::INFO] << "Buffer::Buffer()" << endl;
 }
index d85377f6656ed7b6d21877c35657bebd26aed76b..8fcd49e2689843875dc3730e243d5c621d4c68b9 100644 (file)
@@ -173,7 +173,7 @@ int replaceAll(BufferView * bv,
        if (!searchAllowed(bv, searchstr) || buf.isReadonly())
                return 0;
 
-       recordUndoFullDocument(bv->cursor());
+       recordUndoFullDocument(bv);
 
        MatchString const match(searchstr, cs, mw);
        int num = 0;
index f052b75abf1b3c914b1503f74707beb00ce00527..968d9bd25bd154d2bb4a08acbc02f4ef9fbbc6f6 100644 (file)
@@ -1455,6 +1455,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd)
                }
 
                case LFUN_TEXTCLASS_APPLY: {
+                       recordUndoFullDocument(view());
                        Buffer * buffer = owner->buffer();
 
                        lyx::textclass_type const old_class =
@@ -1573,15 +1574,14 @@ void LyXFunc::sendDispatchMessage(string const & msg, FuncRequest const & cmd)
 
        string const shortcuts = toplevel_keymap->printbindings(cmd);
 
-       if (!shortcuts.empty()) {
+       if (!shortcuts.empty())
                comname += ": " + shortcuts;
-       } else if (!argsadded && !cmd.argument.empty()) {
+       else if (!argsadded && !cmd.argument.empty())
                comname += ' ' + cmd.argument;
-       }
 
        if (!comname.empty()) {
                comname = rtrim(comname);
-               dispatch_msg += '(' + comname + ')';
+               dispatch_msg += '(' + rtrim(comname) + ')';
        }
 
        lyxerr[Debug::ACTION] << "verbose dispatch msg " << dispatch_msg << endl;
index 60735f9503eb3f02c0af2422ef2b50b5457edcbb..e7c3174b2352c746dc08989c7ea3bef382ee5cd3 100644 (file)
@@ -88,7 +88,7 @@ string const latex_path(string const & original_path,
 {
        string path = subst(original_path, "\\", "/");
        path = subst(path, "~", "\\string~");
-       if (path.find(' ') != string::npos)
+       if (path.find(' ') != string::npos) {
                // We can't use '"' because " is sometimes active (e.g. if
                // babel is loaded with the "german" option)
                if (extension == EXCLUDE_EXTENSION) {
@@ -97,11 +97,12 @@ string const latex_path(string const & original_path,
                        // ChangeExtension calls os::internal_path internally
                        // so don't use it to re-add the extension.
                        path = "\\string\"" + base + "\\string\"." + ext;
-               } else
+               } else {
                        path = "\\string\"" + path + "\\string\"";
-       if (dots == ESCAPE_DOTS)
-               return subst(path, ".", "\\lyxdot ");
-       return path;
+               }
+       }
+
+       return dots == ESCAPE_DOTS ? subst(path, ".", "\\lyxdot ") : path;
 }
 
 
@@ -111,19 +112,18 @@ string const MakeLatexName(string const & file)
        string name = OnlyFilename(file);
        string const path = OnlyPath(file);
 
-       for (string::size_type i = 0; i < name.length(); ++i) {
+       for (string::size_type i = 0; i < name.length(); ++i)
                name[i] &= 0x7f; // set 8th bit to 0
-       };
 
        // ok so we scan through the string twice, but who cares.
-       string const keep("abcdefghijklmnopqrstuvwxyz"
+       string const keep = "abcdefghijklmnopqrstuvwxyz"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-               "@!\"'()*+,-./0123456789:;<=>?[]`|");
+               "@!\"'()*+,-./0123456789:;<=>?[]`|";
 
        string::size_type pos = 0;
-       while ((pos = name.find_first_not_of(keep, pos)) != string::npos) {
+       while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
                name[pos++] = '_';
-       }
+
        return AddName(path, name);
 }
 
@@ -149,7 +149,7 @@ bool IsDirWriteable(string const & path)
 {
        lyxerr[Debug::FILES] << "IsDirWriteable: " << path << endl;
 
-       string const tmpfl(tempName(path, "lyxwritetest"));
+       string const tmpfl = tempName(path, "lyxwritetest");
 
        if (tmpfl.empty())
                return false;
@@ -175,7 +175,7 @@ string const FileOpenSearch(string const & path, string const & name,
        while (notfound && !path_element.empty()) {
                path_element = os::internal_path(path_element);
                if (!suffixIs(path_element, '/'))
-                       path_element+= '/';
+                       path_element += '/';
                path_element = subst(path_element, "$$LyX",
                                     package().system_support());
                path_element = subst(path_element, "$$User",
@@ -243,16 +243,12 @@ string const FileSearch(string const & path, string const & name,
        // search first without extension, then with it.
        if (IsFileReadable(fullname))
                return fullname;
-       else if (ext.empty())
+       if (ext.empty())
                return string();
-       else { // Is it not more reasonable to use ChangeExtension()? (SMiyata)
-               fullname += '.';
-               fullname += ext;
-               if (IsFileReadable(fullname))
-                       return fullname;
-               else
-                       return string();
-       }
+       // Is it not more reasonable to use ChangeExtension()? (SMiyata)
+       fullname += '.';
+       fullname += ext;
+       return IsFileReadable(fullname) ? fullname : string();
 }
 
 
@@ -278,8 +274,7 @@ string const LibFileSearch(string const & dir, string const & name,
 }
 
 
-string const
-i18nLibFileSearch(string const & dir, string const & name,
+string const i18nLibFileSearch(string const & dir, string const & name,
                  string const & ext)
 {
        // the following comments are from intl/dcigettext.c. We try
@@ -321,7 +316,7 @@ i18nLibFileSearch(string const & dir, string const & name,
 
 string const LibScriptSearch(string const & command_in)
 {
-       string const token_scriptpath("$$s/");
+       static string const token_scriptpath = "$$s/";
 
        string command = command_in;
        // Find the starting position of "$$s/"
@@ -360,7 +355,7 @@ string const createTmpDir(string const & tempdir, string const & mask)
                << "createTmpDir: tempdir=`" << tempdir << "'\n"
                << "createTmpDir:    mask=`" << mask << '\'' << endl;
 
-       string const tmpfl(tempName(tempdir, mask));
+       string const tmpfl = tempName(tempdir, mask);
        // lyx::tempName actually creates a file to make sure that it
        // stays unique. So we have to delete it before we can create
        // a dir with the same name. Note also that we are not thread
@@ -437,28 +432,22 @@ string const createLyXTmpDir(string const & deflt)
 
 bool createDirectory(string const & path, int permission)
 {
-       string temp(rtrim(os::internal_path(path), "/"));
-
+       string temp = rtrim(os::internal_path(path), "/");
        BOOST_ASSERT(!temp.empty());
-
-       if (mkdir(temp, permission))
-               return false;
-
-       return true;
+       return mkdir(temp, permission) == 0;
 }
 
 
 // Strip filename from path name
-string const OnlyPath(string const & Filename)
+string const OnlyPath(string const & filename)
 {
        // If empty filename, return empty
-       if (Filename.empty()) return Filename;
+       if (filename.empty())
+               return filename;
 
        // Find last / or start of filename
-       string::size_type j = Filename.rfind('/');
-       if (j == string::npos)
-               return "./";
-       return Filename.substr(0, j + 1);
+       string::size_type j = filename.rfind('/');
+       return j == string::npos ? "./" : filename.substr(0, j + 1);
 }
 
 
@@ -472,7 +461,7 @@ string const MakeAbsPath(string const & RelPath, string const & BasePath)
                return RelPath;
 
        // Copies given paths
-       string TempRel(os::internal_path(RelPath));
+       string TempRel = os::internal_path(RelPath);
        // Since TempRel is NOT absolute, we can safely replace "//" with "/"
        TempRel = subst(TempRel, "//", "/");
 
@@ -488,7 +477,7 @@ string const MakeAbsPath(string const & RelPath, string const & BasePath)
                TempBase.erase(TempBase.length() - 2);
 
        // processes relative path
-       string RTemp(TempRel);
+       string RTemp = TempRel;
        string Temp;
 
        while (!RTemp.empty()) {
@@ -500,12 +489,16 @@ string const MakeAbsPath(string const & RelPath, string const & BasePath)
                        // Remove one level of TempBase
                        string::difference_type i = TempBase.length() - 2;
 #ifndef __EMX__
-                       if (i < 0) i = 0;
-                       while (i > 0 && TempBase[i] != '/') --i;
+                       if (i < 0)
+                               i = 0;
+                       while (i > 0 && TempBase[i] != '/')
+                               --i;
                        if (i > 0)
 #else
-                       if (i < 2) i = 2;
-                       while (i > 2 && TempBase[i] != '/') --i;
+                       if (i < 2)
+                               i = 2;
+                       while (i > 2 && TempBase[i] != '/')
+                               --i;
                        if (i > 2)
 #endif
                                TempBase.erase(i, string::npos);
@@ -532,9 +525,7 @@ string const MakeAbsPath(string const & RelPath, string const & BasePath)
 // Chops any path of filename.
 string const AddName(string const & path, string const & fname)
 {
-       // Get basename
-       string const basename(OnlyFilename(fname));
-
+       string const basename = OnlyFilename(fname);
        string buf;
 
        if (path != "." && path != "./" && !path.empty()) {
@@ -569,38 +560,36 @@ bool AbsolutePath(string const & path)
 }
 
 
-
 // Create absolute path. If impossible, don't do anything
 // Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
 string const ExpandPath(string const & path)
 {
        // checks for already absolute path
-       string RTemp(ReplaceEnvironmentPath(path));
+       string RTemp = ReplaceEnvironmentPath(path);
        if (os::is_absolute_path(RTemp))
                return RTemp;
 
        string Temp;
-       string const copy(RTemp);
+       string const copy = RTemp;
 
        // Split by next /
        RTemp = split(RTemp, Temp, '/');
 
-       if (Temp == ".") {
+       if (Temp == ".")
                return getcwd() + '/' + RTemp;
-       }
-       if (Temp == "~") {
+
+       if (Temp == "~")
                return package().home_dir() + '/' + RTemp;
-       }
-       if (Temp == "..") {
+
+       if (Temp == "..")
                return MakeAbsPath(copy);
-       }
+
        // Don't know how to handle this
        return copy;
 }
 
 
-// Normalize a path
-// Constracts path/../path
+// Normalize a path. Constracts path/../path
 // Can't handle "../../" or "/../" (Asger)
 // Also converts paths like /foo//bar ==> /foo/bar
 string const NormalizePath(string const & path)
@@ -613,7 +602,7 @@ string const NormalizePath(string const & path)
                RTemp = path;
        else
                // Make implicit current directory explicit
-               RTemp = "./" +path;
+               RTemp = "./" + path;
 
        // Normalise paths like /foo//bar ==> /foo/bar
        boost::RegEx regex("/{2,}");
@@ -832,13 +821,13 @@ string const getFormatFromContents(string const & filename)
                return string();
 
        // gnuzip
-       string const gzipStamp = "\037\213";
+       static string const gzipStamp = "\037\213";
 
        // PKZIP
-       string const zipStamp = "PK";
+       static string const zipStamp = "PK";
 
        // compress
-       string const compressStamp = "\037\235";
+       static string const compressStamp = "\037\235";
 
        // Maximum strings to read
        int const max_count = 50;
@@ -857,7 +846,7 @@ string const getFormatFromContents(string const & filename)
                }
 
                getline(ifs, str);
-               string const stamp = str.substr(0,2);
+               string const stamp = str.substr(0, 2);
                if (firstLine && str.size() >= 2) {
                        // at first we check for a zipped file, because this
                        // information is saved in the first bytes of the file!
@@ -1008,8 +997,7 @@ string const unzipFile(string const & zipped_file, string const & unzipped_file)
 string const MakeDisplayPath(string const & path, unsigned int threshold)
 {
        string str = path;
-
-       string const home(package().home_dir());
+       string const home = package().home_dir();
 
        // replace /home/blah with ~/
        if (prefixIs(str, home))
@@ -1084,9 +1072,9 @@ cmd_ret const RunCommand(string const & cmd)
 
        // (Claus Hentschel) Check if popen was succesful ;-)
        if (!inf) {
-               return make_pair(-1, string());
                lyxerr << "RunCommand:: could not start child process" << endl;
-               }
+               return make_pair(-1, string());
+       }
 
        string ret;
        int c = fgetc(inf);
@@ -1194,8 +1182,7 @@ string const readBB_from_PSFile(string const & file)
        // %%BoundingBox: (atend)
        // In this case we must check the end.
        bool zipped = zippedFile(file);
-       string const file_ = zipped ?
-               string(unzipFile(file)) : string(file);
+       string const file_ = zipped ?  unzipFile(file) : file;
        string const format = getFormatFromContents(file_);
 
        if (format != "eps" && format != "ps") {
@@ -1220,7 +1207,8 @@ string const readBB_from_PSFile(string const & file)
 
 int compare_timestamps(string const & file1, string const & file2)
 {
-       BOOST_ASSERT(AbsolutePath(file1) && AbsolutePath(file2));
+       BOOST_ASSERT(AbsolutePath(file1));
+       BOOST_ASSERT(AbsolutePath(file2));
 
        // If the original is newer than the copy, then copy the original
        // to the new directory.
index fb7af0a7328a373cd9c33ce41dcba1598c88b39a..7a551ac02bf074e733124461c7d5a5aa440b3234 100644 (file)
@@ -492,7 +492,7 @@ vector<string> const getVectorFromString(string const & str,
        vector<string> vec;
        if (str.empty())
                return vec;
-       string keys(rtrim(str));
+       string keys = rtrim(str);
        for(;;) {
                string::size_type const idx = keys.find(delim);
                if (idx == string::npos) {
index bb600dd118c6fa7da63bde18e2e4dd68c311f4fc..4afddcbaf41d235cbeba27eccf39c43a0873f98d 100644 (file)
@@ -48,10 +48,12 @@ std::ostream & operator<<(std::ostream & os, Undo const & undo)
 }
 
 
-void recordUndo(Undo::undo_kind kind,
-       DocIterator & cell,
+void doRecordUndo(Undo::undo_kind kind,
+       DocIterator const & cell,
        pit_type first_pit, pit_type last_pit,
-       DocIterator & cur,
+       DocIterator const & cur,
+       BufferParams const & bparams,
+       bool isFullBuffer,
        limited_stack<Undo> & stack)
 {
        if (first_pit > last_pit)
@@ -61,6 +63,8 @@ void recordUndo(Undo::undo_kind kind,
        undo.kind = kind;
        undo.cell = cell;
        undo.cursor = cur;
+       undo.bparams = bparams ;
+       undo.isFullBuffer = isFullBuffer;
        lyxerr << "recordUndo: cur: " << cur << endl;
        lyxerr << "recordUndo: pos: " << cur.pos() << endl;
        //lyxerr << "recordUndo: cell: " << cell << endl;
@@ -87,7 +91,7 @@ void recordUndo(Undo::undo_kind kind,
                // some more effort needed here as 'the whole cell' of the
                // main LyXText _is_ the whole document.
                // record the relevant paragraphs
-               LyXText * text = cell.text();
+               LyXText const * text = cell.text();
                BOOST_ASSERT(text);
                ParagraphList & plist = text->paragraphs();
                ParagraphList::iterator first = plist.begin();
@@ -113,15 +117,45 @@ void recordUndo(Undo::undo_kind kind,
        BOOST_ASSERT(first_pit <= cur.lastpit());
        BOOST_ASSERT(last_pit <= cur.lastpit());
 
-       recordUndo(kind, cur, first_pit, last_pit, cur, stack);
+       doRecordUndo(kind, cur, first_pit, last_pit, cur,
+               cur.bv().buffer()->params(), false, stack);
 }
 
 
-void performUndoOrRedo(BufferView & bv, Undo const & undo)
+
+// Returns false if no undo possible.
+bool textUndoOrRedo(BufferView & bv,
+       limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
 {
+       finishUndo();
+
+       if (stack.empty()) {
+               // Nothing to do.
+               return false;
+       }
+
+       // Adjust undo stack and get hold of current undo data.
+       Undo undo = stack.top();
+       stack.pop();
+
+       // We will store in otherstack the part of the document under 'undo'
+       Buffer * buf = bv.buffer();
+       DocIterator cell_dit = undo.cell.asDocIterator(&buf->inset());
+
+       doRecordUndo(Undo::ATOMIC, cell_dit,
+                  undo.from, cell_dit.lastpit() - undo.end, bv.cursor(),
+                        undo.bparams, undo.isFullBuffer,
+                  otherstack);
+
+       // This does the actual undo/redo.
        //lyxerr << "undo, performing: " << undo << std::endl;
-       DocIterator dit = undo.cell.asDocIterator(&bv.buffer()->inset());
-       if (dit.inMathed()) {
+       DocIterator dit = undo.cell.asDocIterator(&buf->inset());
+       if (undo.isFullBuffer) {
+               // This is a full document
+               otherstack.top().bparams = buf->params();
+               buf->params() = undo.bparams;
+               buf->paragraphs() = undo.pars;
+       } else if (dit.inMathed()) {
                // We stored the full cell here as there is not much to be
                // gained by storing just 'a few' paragraphs (most if not
                // all math inset cells have just one paragraph!)
@@ -148,42 +182,18 @@ void performUndoOrRedo(BufferView & bv, Undo const & undo)
                ParagraphList::const_iterator pit = undo.pars.begin();
                ParagraphList::const_iterator end = undo.pars.end();
                for (; pit != end; ++pit)
-                       const_cast<Paragraph &>(*pit).setInsetOwner(dynamic_cast<UpdatableInset *>(&dit.inset()));
+                       const_cast<Paragraph &>(*pit).setInsetOwner(
+                               dynamic_cast<UpdatableInset *>(&dit.inset()));
                plist.insert(first, undo.pars.begin(), undo.pars.end());
        }
 
+       // Set cursor
        LCursor & cur = bv.cursor();
-       cur.setCursor(undo.cursor.asDocIterator(&bv.buffer()->inset()));
+       cur.setCursor(undo.cursor.asDocIterator(&buf->inset()));
        cur.selection() = false;
        cur.resetAnchor();
        finishUndo();
-}
-
-
-// Returns false if no undo possible.
-bool textUndoOrRedo(BufferView & bv,
-       limited_stack<Undo> & stack, limited_stack<Undo> & otherstack)
-{
-       finishUndo();
 
-       if (stack.empty()) {
-               // Nothing to do.
-               return false;
-       }
-
-       // Adjust undo stack and get hold of current undo data.
-       Undo undo = stack.top();
-       stack.pop();
-
-       // We will store in otherstack the part of the document under 'undo'
-       DocIterator cell_dit = undo.cell.asDocIterator(&bv.buffer()->inset());
-
-       recordUndo(Undo::ATOMIC, cell_dit,
-                  undo.from, cell_dit.lastpit() - undo.end,
-                  bv.cursor(), otherstack);
-
-       // This does the actual undo/redo.
-       performUndoOrRedo(bv, undo);
        return true;
 }
 
@@ -256,8 +266,17 @@ void recordUndo(LCursor & cur, Undo::undo_kind kind,
 }
 
 
-void recordUndoFullDocument(LCursor & cur)
+void recordUndoFullDocument(BufferView * bv)
 {
-       recordUndo(Undo::ATOMIC, cur, 0,
-                  cur.bv().text()->paragraphs().size() - 1);
+       Buffer * buf = bv->buffer();
+       doRecordUndo(
+               Undo::ATOMIC,
+               doc_iterator_begin(buf->inset()),
+               0, buf->paragraphs().size() - 1,
+               bv->cursor(),
+               buf->params(),
+               true,
+               buf->undostack()
+       );
+       undo_finished = false;
 }
index 7b4aca1c92a27b8bbb0035e49a386d873c7feb2c..781621c5bb67ce5fb4e6d78d9d13ccb0c5e28efc 100644 (file)
 
 #include "dociterator.h"
 #include "ParagraphList_fwd.h"
+#include "bufferparams.h"
 
 #include "support/types.h"
 
 #include <string>
 
-class LCursor;
+class BufferParams;
 class BufferView;
+class LCursor;
 
 
 /**
@@ -81,6 +83,10 @@ public:
        ParagraphList pars;
        /// the stringified contents of the saved MathArray (for mathed)
        std::string array;
+       /// Only used in case of full backups
+       BufferParams bparams;
+       /// Only used in case of full backups
+       bool isFullBuffer;      
 };
 
 
@@ -116,7 +122,7 @@ void recordUndoInset(LCursor & cur, Undo::undo_kind kind = Undo::ATOMIC);
 /// Convenience: prepare undo for the selected paragraphs
 void recordUndoSelection(LCursor & cur, Undo::undo_kind kind = Undo::ATOMIC);
 
-/// Convenience: prepare undo for the single paragraph containing the cursor
-void recordUndoFullDocument(LCursor & cur);
+/// Convenience: prepare undo for the whole buffer
+void recordUndoFullDocument(BufferView * bv);
 
 #endif // UNDO_FUNCS_H