2

The goal is to prevent the possibility of undoing some automatic text additions into buffer.

So far I tried to solve it by:

    (defun skip-undo (orig-fun)
      (let ((buffer-undo-list t))
        (apply orig-fun nil)))
    
    (advice-add 'fun-that-adds-some-text :around #'skip-undo)

It works. But when fun-that-adds-some-text adds some text, all positions after that addition in the buffer will be affected. And all of the undos after this position will be broken.

How could it be solved?

4
  • Short of horribly abusing the undo-in-region functionality, I don't know how you'd approach that. Why do you want to? What is the nature of the text?
    – phils
    Mar 20 at 4:37
  • In certain cases, it is necessary to add HASH checksums with the TIMESTAMP of the entry body to the PROPERTY drawer. And then it would be better not to accidentally undo it.
    – Herman
    Mar 20 at 12:27
  • I guess you're talking about org-mode. I'd suggest that it's preferable not to accidentally undo anything that you didn't mean to undo, so I don't understand why this is different to any other change. My only suggestion is that you additionally write the information to a log file that you're not editing, and that way you have a backup record which isn't subject to accidental keystrokes. I have no solution for what you're actually asking for, though.
    – phils
    Mar 20 at 12:50

1 Answer 1

1

The solution was easier than I thought. With the following solution, all changes can be sequentially restored with undo, except those made by fun-that-adds-some-text.

(defun skip-undo (orig-fun &rest args)
  (undo-boundary)
  (apply orig-fun args)
  (setq buffer-undo-list (memq nil buffer-undo-list)))
    
(advice-add 'fun-that-adds-some-text :around #'skip-undo)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.