Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to prevent Emacs from setting an undo boundary?
    text
    copied!<p>I've written an Emacs Lisp function which calls a shell command to process a given string and return the resulting string. Here is a simplified example which just calls <code>tr</code> to convert text to uppercase:</p> <pre><code>(defun test-shell-command (str) "Apply tr to STR to convert lowercase letters to uppercase." (let ((buffer (generate-new-buffer "*temp*"))) (with-current-buffer buffer (insert str) (call-process-region (point-min) (point-max) "tr" t t nil "'a-z'" "'A-Z'") (buffer-string)))) </code></pre> <p>This function creates a temporary buffer, inserts the text, calls <code>tr</code>, replaces the text with the result, and returns the result.</p> <p>The above function works as expected, however, when I write a wrapper around this function to apply the command to the region, two steps are being added to the undo history. Here's another example:</p> <pre><code>(defun test-shell-command-region (begin end) "Apply tr to region from BEGIN to END." (interactive "*r") (insert (test-shell-command (delete-and-extract-region begin end)))) </code></pre> <p>When I call <code>M-x test-shell-command-on-region</code>, the region is replaced with the uppercase text, but when I press <code>C-_</code> (<code>undo</code>), the first step in the undo history is the state with the text deleted. Going two steps back, the original text is restored.</p> <p>My question is, how does one prevent the intermediate step from being added to the undo history? I've read the <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Undo.html">Emacs documentation on undo</a>, but it doesn't seem to address this as far as I can tell.</p> <p>Here's a function which accomplishes the same thing by calling the built-in Emacs function <code>upcase</code>, just as before: on the result of <code>delete-and-extract-region</code> with the result being handed off to <code>insert</code>:</p> <pre><code>(defun test-upcase-region (begin end) "Apply upcase to region from BEGIN to END." (interactive "*r") (insert (upcase (delete-and-extract-region begin end)))) </code></pre> <p>When calling <code>M-x test-upcase-region</code>, there is only one step in the undo history, as expected. So, it seems to be the case that calling <code>test-shell-command</code> creates an undo boundary. Can that be avoided somehow?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload