3

This question already has an answer here:

Consider this toy example:

(defun foo ()
  (interactive)

  (replace-string "a" "A" nil (region-beginning) (region-end) nil)
  (replace-string "r" "R" nil (region-beginning) (region-end) nil)
)

(global-set-key (kbd "C-c F") 'foo)

The function foo replaces all a's and r's with A's and R's, respectively. If one wants to undo it, however, a single undo (C-x u) isn't be enough; two are required.

I'm looking for a way to define foo such that only one C-x u is enough to undo everything it does.

Since the code above is only an example, I'm looking for solutions that do not depend on the specifics of replace-string. IOW, the solution should work for any foo of the form

(defun foo ()
  (interactive)

  ;;
  ;; body
  ;;
)

marked as duplicate by Kaushal Modi, wasamasa, Francesco, sds, Tyler Sep 22 '15 at 21:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.