reddit / emacs

Create random indentation?

t3lp3rion · 2022-04-19 10:15
Hi, I am teaching c++ programming and one of the parallel things I am trying to transmit is the importance of not only a clear logic but also of a readable code, which includes proper indentation. In spite of the many ways to indent code that are present in emacs, vscode , etc, or even using simple tools like clang-format , students still manage to create and present code which is just not organized, putting everything at the same 0 spaces level or similar. Since sometimes I give them a template to work on, I want to mess with it by removing proper indentation automatically, so I would like to know if there is a way to "random indent" a file in emacs, like adding four space to one line, then zero spaces to the next line, then two and so on, so they will be forced to organize it. Thanks
T_Verron · 2022-04-19 10:38
Something like this? (defun random-indent-buffer () (interactive) (while (not (eobp)) (let ((beg (progn (beginning-of-line) (point))) (end (progn (back-to-indentation) (point)))) (delete-region beg end) (while (> (random* 3) 0) (insert " ")) (forward-line 1)))) But for your purpose, I think removing all indentation should be enough. Edit: or just copy and paste your code in reddit, apparently. :)
t3lp3rion · 2022-04-19 10:45
Thank you! I will also test with removing indent by using rigid-indent has the same effect :)
qgz · 2022-04-19 14:51
You are doing a great thing for society here, I'm being completely serious
t3lp3rion · 2022-04-19 17:18
Thank you! It is also very useful for me when grading :)