1

Sometimes I make whitespace cleanups to source code. I'd like to be certain that I do not accidentally introduce real code changes (let us assume that the programming language is one where removing or adding white space never changes the meaning of code).

Is there a minor mode that sets Emacs to only allow whitespace changes in a particular buffer? So I'd be able to delete white space characters (space, tab, newline etc) and add ones, but not make any other alterations to the buffer content.

(A more advanced mode would allow changes in the length of white space sequences, and allow introducing and deleting it around punctuation characters, but disallow breaking a word token (defined as, say, a sequence of characters in [A-Za-z0-9_]) by adding new spaces in the middle, nor fusing two separate word tokens by deleting the white space that separated them. But this is not essential. Most programming languages will make it pretty obvious if you accidentally change 'int x;' to 'intx;'.)

0

While I personally use -- and feel like the general trend is towards -- external tooling (à la clang-format/clang-tidy, autopep8, perltidy, rustfmt, etc.) Emacs has some useful tools.

While it doesn't restrict editing, have you looked at the excellent whitespace-cleanup (and its cousin whitespace-cleanup-region) that comes with Emacs? Based on your settings for whitepsace-style, indent-tabs-mode, and tab-width it can delete whitespace at the beginning or end of the buffer or lines, compress and expand whitespace and swap around tabs and spaces. It is part of whitespace.el and you can find its documentation by doing C-h f whitespace-cleanup RET. You could add it to before-save-hook to run at every save, or install a wrapper to possibly save yourself some grief.

My personal favorite is delete-trailing-whitespace which does exactly that; it deletes the trailing whitespace on lines (and optionally and by default) the buffer.

Also, check the documentation for the major mode of the language you're using. Often they have baked in functions for editing/refactoring whitespace.

Additionally the Emacs wiki has a solid article on deleting whitespace, while the Emacs manual has an entry on useless whitespace. The elisp manual has a page on some whitespace deleting functions

| improve this answer | |
  • I should also mention dtrt-indent. While not necessarily great for refactoring, it tries to "do the right thing" by adjusting your buffer-local indentation settings when loading files from a foreign codebase, helping to keep your diffs clean. – nega Nov 21 '19 at 18:31
0

I've run into the same issue. Besides using auto-formatters (which have mixed results - some aren't entirely lossless).

  • Compare the resulting object code (bytecode, generated AST or assembly), this isn't fool proof if the language uses a preprocessor, nevertheless, it can help. (example for Python, and Rust)
  • Write a simple function that removes or normalizes all white-space, then compares output. (By normalize I mean converts all kinds of space to a single space or newline for example).

    While this may be worse in some cases, it will pick up changes missed by a pre-processor.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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