I achieved what I want through some (not that many) manual steps.
- find out what is the maximum number of tabs (or sets of 4 spaces), say it's 3
- open the search and replace window, input 3 tabs (or 12 spaces), and click "Find All"
- now all groups of 3 tabs are selected, and you are working on all the lines with maximum indentation
- park or close the search and replace window, click 3 times on the "Increase indent" button (or set the left indentation in the paragraph style menu)
- delete the selected groups of 3 tabs
- open the search and replace window, input 2 tabs (or 8 spaces), and click "Find All"
- park or close the search and replace window, click 2 times on the "Increase indent" button
- delete the selected groups of 2 tabs
- open the search and replace window, input 1 tabs (or 4 spaces), and click "Find All"
- park or close the search and replace window, click 1 time on the "Increase indent" button
- delete the selected single tabs
Now you have a nice code indentation.
If you are using Microsoft Word, then there's no "find all", but there is a way to apply paragraph styles directly from the search and replace menu. But the steps are a little different.
First decide how wide a single indentation should be (e.g. 0.5 cm)
- open the find and replace window, input 3 tabs (or 12 spaces) in the Find bar
- leave the Replace bar empty, but click on it
- if you don't see the Search options group, click More
- click on Format
- click on Paragraph
- set a left indentation of 3 * the indentation width you want (e.g. 1.5 cm)
- click Replace All, the paragraph style will be applied but the tabs/spaces will NOT be removed
- click on the empty Replace bar (again)
- click No Formatting
- click on Replace All (again)
- now the tabs/spaces will be deleted
Rinse and repeat until you get a nice indentation.
If you are using Python (or if you want to keep your white spaces) then instead of deleting the tabs (or spaces), you can replace them with a placeholder character you don't use in the rest of the code, say £ and replace them back in one pass when you are done. However, you'll get a skewed indentation.
I guess there's a way to do this with macros, but this was good enough for me.