syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.62k stars 4.89k forks source link

Persistent undo #774

Closed bandali0 closed 3 weeks ago

bandali0 commented 9 years ago

In vim, I can use something like this

set directory=~/.vim/swaps
if exists("&undodir")
    set undodir=~/.vim/undo
endif
set undofile

in my vimrc and have my undo history preserved. So, for instance if I make some changes and save and close vim. If later on I change my mind, I can open vim again, open the file and just undo (u) whatever I had done.

Using spacemacs, if I exit emacs, my undo history will be gone; meaning the next time I open emacs and open the file, pressing u does nothing.

Is there a way to have the undo history preserved, across launches?

tuhdo commented 9 years ago

Yes, you can do it by setting undo-tree-auto-save-history variable to true. You can also save/load manually with undo-tree-save-history and undo-tree-load-history.

syl20bnr commented 9 years ago

The temp directory is ~/.emacs.d/.cache, the persistent undo was enabled once but has been disabled because some people (and I) reported corrupted undo history. Not sure it is due to persistency, you can try it for yourself.

bandali0 commented 9 years ago

@tuhdo That's what I'm looking for, thank you :beers:

@syl20bnr Thanks, that's good to know. I just saw #298.

I've added this

(setq undo-tree-auto-save-history t
         undo-tree-history-directory-alist
         `(("." . ,(concat spacemacs-cache-directory "undo"))))
   (unless (file-exists-p (concat spacemacs-cache-directory "undo"))
(make-directory (concat spacemacs-cache-directory "undo")))

(which is the reverse of 885d092) to my dotspacemacs/config and so far I haven't had issues.

Let's leave this issue open for a while to see if I get corrupted history or not. If everything works well for a week or two, I'll close it.

syl20bnr commented 9 years ago

Great ! Thank you for testing this.

bandali0 commented 9 years ago

@syl20bnr No worries, thank you for spacemacs :)

bandali0 commented 9 years ago

@syl20bnr It's been 10 days and I haven't had any issues so far.

Although I use emacs regularly, I haven't spent hours editing one single file to see if the history goes corrupt or not. So, that's the last thing I'll try to test this: Soon I'll have to write a lab report. I use emacs for editing TeX, and writing the report would at least take a good couple of hours, so that's my last shot of trying to get a corrupt undo tree.

syl20bnr commented 9 years ago

Any news on this ?

bandali0 commented 9 years ago

@syl20bnr I haven't had any undo history corruptions. I think it's safe to add the feature back, or at least make it an option (disabled by default) that one can set in .spacemacs

ReneFroger commented 9 years ago

Maybe my issue might be related.

If you make a change somewhere, save it, and make a change, then save again, then move your cursor. Press then g;. Are you going back to the correspodenting changed places three in time?

syl20bnr commented 9 years ago

That's a nice update on this issue. Thank you ! :+1:

ReneFroger commented 9 years ago

Any news on this, @aminb?

bandali0 commented 9 years ago

@ReneFroger When I did g; status bar said "No further change info". :confused:

ReneFroger commented 9 years ago

So it causes a bug. Then it cannot be included in Spacemacs, until we get a update from the authors of Evil, I guess.

bandali0 commented 9 years ago

Fair enough :)

sooheon commented 9 years ago

FWIW, @ReneFroger's bug may not be caused by this alone, as I'm seeing the bug without this customization.

Kethku commented 9 years ago

I added @aminb's change and I have been getting a corrupt undo tree message periodically, so as far as I can tell it is still a problem unfortunately. Optionally we could add it back as an optional flag in the init file? I have a hunch that it only happens in some major modes, but I don't really have any evidence for that hunch.

syl20bnr commented 9 years ago

Did you work on a source controlled repo ? I wonder if messing with the buffer content could corrupt the undo tree state.

Kethku commented 9 years ago

That is very possible. I don't think I ever write text or code in a non git or perforce backed directory, so that could be the issue.

syl20bnr commented 9 years ago

Interesting, I'm not sure if the error happened only when I was at work but it happens that I'm using perforce too.

ReneFroger commented 9 years ago

I don't use perforce or another source controlled repo. Is there any way to get persistent undo and last-changes working?

qizhihere commented 9 years ago

What about undohist? I just try it, very nice! But i don't know if i will encounter corruptions like in undotree...

ReneFroger commented 9 years ago

Are you sure that the undo's are persistent after closing and starting Emacs? I tested it, and it didn't worked for me. I use Windows by the way.

Kethku commented 9 years ago

They aren't currently. You need to manually enable it to get the functionality.

Sent from my Windows Phone


From: ReneFrogermailto:notifications@github.com Sent: ‎8/‎16/‎2015 12:03 PM To: syl20bnr/spacemacsmailto:spacemacs@noreply.github.com Cc: Keith Simmonsmailto:keith@the-simmons.net Subject: Re: [spacemacs] Persistent undo (#774)

Are you sure that the undo's are persistent after closing and starting Emacs? I tested it, and it didn't worked for me. I use Windows by the way.

— Reply to this email directly or view it on GitHubhttps://github.com/syl20bnr/spacemacs/issues/774#issuecomment-131603458.

qizhihere commented 9 years ago

Yes, it is. I use it with desktop-save-mode.

ReneFroger commented 8 years ago

@Devagamster @qizhihere could you tell more about how you set it up, please? I couldn't get undohist working. And it's a feature that I miss a lot in Emacs.

qizhihere commented 8 years ago

oh, i just found that undohist may also cause undo history corruption sometimes. Anyway, this is my configuration:

(setq desktop-dirname             "~/.emacs.d/session/"
      desktop-path                (list desktop-dirname)
      desktop-files-not-to-save   "^$"
      desktop-restore-eager 5) ;; lazily restore desktop buffers
(desktop-save-mode 1)

(undohist-initialize)

You can find more info in Emacs Wiki.

ReneFroger commented 8 years ago

Still not working here, unfortunately. I have the undohist and desktop-mode enabled in my config, same for undohist-initialize with your code. But still not working.

ReneFroger commented 8 years ago

Reminds me of this: http://steve-yegge.blogspot.nl/2008/04/xemacs-is-dead-long-live-xemacs.html

Emacs, like Firefox, can be configured to back up your state periodically, so that in theory it can recover after a crash. That's part of the problem: you didn't actually have to configure Firefox to get that behavior. It does it automatically. And to be honest, I've never had much luck with the Emacs save-state facilities. I'm a pretty competent elisp hacker these days, but the desktop.el has never worked reliably for me. I could probably get it to work, but I've always found it easier to write specialized startup "scripts" (lisp functions) that load up particular favorite configurations.

If I can't get desktop-save working, I'd guess that fewer than 1/10th of 1 percent of Emacs users could use that feature.

a13ph commented 8 years ago

My own thinking on this left me with idea of setting up automatic saving of commit ot git-wip and then trying to figure out how to make undo-tree and git time machine friends, if that would be needed. Or just make emacs know that he can undo from git data. Hacky, but uniform.

noctuid commented 8 years ago

@a13ph That seems like a pretty complicated workaround. I just wish undo-tree-auto-save-history (turned on) worked even most of the time. For me, it means I usually have 3-4 undos before I hit some corrupted entry, and I constantly get corrupted undo files that cause emacs to crash when visiting the corresponding file.

The last commit to undo tree (almost 2 years ago) had to do with this issue which mentioned the "Unrecognized entry in undo list undo-tree-canary" error. However, it only fixed the other part of the problem (the hanging). Does anyone know if there has been any discussion with the undo tree author since then?

Also, has anyone tried (add-hook 'kill-emacs-hook #'undo-tree-save-history)? I'm going to try this to see if only saving the history when exiting emacs fixes some of the problems. g; is broken for me even when I have undo-tree-auto-save-history set to nil.

Edit: I didn't realize those were for only one buffer. Has anyone tried keeping (add-hook 'find-file-hook #'undo-tree-load-history-hook) and using something other than write-file-functions for undo-tree-save-history? I'll try this out.

Edit2: So something along the lines of this:

(defun my-save-undo-history ()
  (when (and (boundp 'undo-tree-mode)
             undo-tree-mode
             buffer-file-name
             (file-writable-p buffer-file-name)
             (not (eq buffer-undo-list t))
             (not revert-buffer-in-progress-p))
    (undo-tree-save-history nil t)))

(defun my-save-all-undo-history ()
  (dolist (buffer (buffer-list))
    (with-current-buffer buffer
      (my-save-undo-history))))

(add-hook 'kill-emacs-hook #'my-save-all-undo-history)
(add-hook 'kill-buffer-hook #'my-save-undo-history)

I will test this. I think maybe the reason I was getting so much corruption is because I was automatically saving so much.

a13ph commented 8 years ago

@noctuid to be fair, it's not as much a workaround, as a personal quirky thinking on the issue among other related issues, in context. So this might be too opinionated, I agree.

I'll await for PR and resolution to this, to try if it would be enough for me, but git and undo integration is such an obvious target to me, I'll probably be tempted by it anyways.

noctuid commented 8 years ago

This has fixed most of my issues. I didn't have any corruption until today where I got a corrupted undo file that crashed emacs. This doesn't actually solve the issue I guess; it just makes it happen a lot less.

qizhihere commented 8 years ago

@noctuid Me too. I haven't encountered any history corruption since i have replaced undohist with undo-tree's auto-save-history feature. BTW, you don't need to bind hooks manually, (setq undo-tree-auto-save-history t) is enough. Besides, ensure that undo-tree-mode is enabled before desktop-read if you have enabled desktop-save-mode.

Here is my latest config:

(setq desktop-path (list (my:init-dir "session/"))
      desktop-auto-save-timeout 5
      desktop-restore-eager 5) ;; lazily restore desktop buffers
(desktop-save-mode 1)

;; add before `desktop-read' hook
(defvar desktop-before-read-hook '() "Hooks run before `desktop-read'.")
(defadvice desktop-read (before my:desktop-read-run-before-hooks activate)
  (run-hooks 'desktop-before-read-hook))

(setq undo-tree-auto-save-history t)
(defun my:enable-global-undo-tree()
  (global-undo-tree-mode 1)
  (diminish 'undo-tree-mode))
(add-hook 'desktop-before-read-hook 'my:enable-global-undo-tree)
noctuid commented 8 years ago

@qizhihere Like I said before, I get corruption constantly with undo-tree-auto-save-history (which hooks onto every save) unfortunately. That's why I started using those hooks to only save when killing buffers and closing emacs, and it's mostly fixed the issue for me. It seems that some people don't have a problem with undo-tree-auto-save-history, but enough people do for it to be a serious issue I think. Ideally, there would be no corruption problems in the first place.

qizhihere commented 8 years ago

@noctuid Eh...I misunderstood. So sorry :)

a13ph commented 8 years ago

report upstream corruption issues?

noctuid commented 8 years ago

It was reported about two years ago.

toupeira commented 8 years ago

It seems that some people don't have a problem with undo-tree-auto-save-history

I wonder if it's related to the OS/filesystem or something like that.

olejorgenb commented 8 years ago

I just got hit by this data-loss causing corruption again. Doesn't happen very often, but very annoying when it does. I have undo-tree-auto-save-history on. Will try to disable it and see if get the issue again.

Maybe relevant: https://lists.gnu.org/archive/html/bug-gnu-emacs/2014-01/msg00282.html

I have the persisted undo tree file and the corresponding file with the corruption if anyone thing that might be helpful

mrkgnao commented 7 years ago

Is anyone working on this?

djm commented 7 years ago

I have undo-tree-auto-save-history on and just lost some more work due to this. I'll be turning it off for the mean time.

It only seems to happen when I'm going far back in history, I hold down the u key to go back to an older place in time to yank something, but then when I come back by holding down ctrl +r it gets stuck on a certain undo phase and I can never get back to my newest changes.

I don't get this in every day usage of undo-ing and redo-ing, it seems to only either be cases where a) I go back further than usual or b) hold down the u key. Possible race issue?

Stebalien commented 7 years ago

I have undo-tree-auto-save-history on and just lost some more work due to this. I'll be turning it off for the mean time.

I have the same problem but have undo-tree-auto-save-history off.

yqrashawn commented 7 years ago

any update on this?

apirogov commented 7 years ago

I would really love to see reliable persistent undo in spacemacs. In vim a few times it saved me when I messed up a file and could find a version from hours ago after having closed and opened it multiple times in between. Until this bug is hunted down though, I am afraid of enabling the auto-save-history flag after reading this thread here.

braham-snyder commented 7 years ago

Unfortunately, github doesn't have issue voting, but in the meantime I believe giving the original post here a "thumbs up" reaction is the way to go (issues can be sorted by the number of reactions received).

webavant commented 7 years ago

Undo-tree's auto-saving was working fine for me until I began building emacs from the master development repo. I downgraded to a recent third-party build from master and my seemingly broken .file~undo-tree~ files began working again, despite that they had been problematic only moments before downgrading emacs. Here is a comparison of my configuration :

< In GNU Emacs 26.0.50.2 (x86_64-unknown-linux-gnu, GTK+ Version 3.22.6)
<  of 2017-01-09 built on mbp
< Repository revision: ef8c9f8fc922b615aca91b47820d1f1900fddc96
> In GNU Emacs 26.0.50.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.22.3)
>  of 2016-11-14 built on lilydjwg
> Repository revision: 2f5e0b1bf7b0ac4f450847db34d599a072020600
<  --without-gconf --with-gsettings --with-xwidgets 'CFLAGS=-march=x86-64
<  -mtune=generic -O2 -pipe -fstack-protector-strong'
<  CPPFLAGS=-D_FORTIFY_SOURCE=2
>  --without-gconf --with-gsettings 'CFLAGS=-march=x86-64 -mtune=generic
>  -O2 -pipe -fstack-protector-strong' CPPFLAGS=-D_FORTIFY_SOURCE=2
< GTK3 X11 MODULES XWIDGETS LIBSYSTEMD
> GTK3 X11 MODULES
<   locale-coding-system: utf-8-unix
>   locale-coding-system: utf-8-unix`

(I am not sure what produced the back-tick on the final line)

webavant commented 7 years ago

You can simplify tests in your environment by setting a few command-line options:

-Q to bypass init.el -L to load undo-tree.el --eval to set the undo-tree settings variables

Example:

emacs -Q -L ~/.emacs.d/elpa/undo-tree-* --execute "(progn(require 'undo-tree)(setq undo-tree-auto-save-history t)(global-undo-tree-mode))" /your/problem/file

therealpxc commented 7 years ago

I noticed that a user named @razzius made a little layer for persistent undo in his personal config, based on undohist. I decided to give it a try in my own config, and it seems to at least work. Is anyone else still interested in considering this as a candidate replacement? (Naturally there is more work to do.)

I'll continue using it for a while and report back. If we could hear from @razzius about the tool's functionality and stability (since it has been in his config for about a year now), that would be awesome too. It looks like we haven't heard anything from @CestDiego since the submission of his undohist PR, either.

razzius commented 7 years ago

@therealpxc It works fine but honestly I restart emacs so infrequently it isn't all that useful to me. Back when I used Vim I would open a new editor every few minutes but in Emacs I keep a single editor server running for days at a time.

There was 1 time when undo glitched but I'm not even sure that was due to undohist.

The other potential downside is dozens of messages about the undo history being discarded due to the file digest not matching on startup, and I presume it's adding to my (already slow) Emacs startup time.

undohist is functional and seems to be the de facto way to persist undo history in Emacs so I think it's a neat feature that could be added but disabled by default.

noctuid commented 7 years ago

Lawlist has been working on an undo-tree fork.