1

I want to convert file created without (org-indent-mode) to (org-indent-mode) format. Mostly it's additional 2,3,4 spaces. I was trying to fix it with string-replace. But maybe somebody have better idea? That's example part: enter image description here

And with turned on (org-indent-mode)

enter image description here

4
  • 1
    org-indent-mode is not a "format": it does not physically add spaces to your file. It's a minor mode that dynamically does "virtual" indentation, so it changes the appearance of the buffer. Try opening the file and doing M-x org-indent-mode once or twice and see what happens.
    – NickD
    Commented Aug 13, 2022 at 21:03
  • I understand. But without org-indent-mode I was creating files like on first picture - text under headlines is indent by added spaces. And now when I turned on org-indent-mode which indention is not based on spaces & tabs - file look like on second picture. It's not properly align. So I have to remove all previously added spaces & tabs.
    – slk500
    Commented Aug 14, 2022 at 6:16
  • In the first headline is that a single list with four items or is it a two-level list with one item on the first level and three items in the second level? To me, it seems to be the latter, in which case, it is rendered correctly and you don't want to change anything. OTOH, the contents of the second and third headline have extra spaces which need to be removed. If that is the case, it will be very hard to make the changes that you want using string matching because you will not have the context: you'd need a full org-mode parser to be able to figure out in which context to make the change.
    – NickD
    Commented Aug 14, 2022 at 17:49
  • Personally, I would worry about it a file at a time: when you open the file, assess the situation and fix it up by hand (or with a small set of heuristics which you might want to collect in a file for further use if you think you'll encounter the same situation again).
    – NickD
    Commented Aug 14, 2022 at 17:54

2 Answers 2

2

I roughly made two functions that add or remove hard indentations.

(defun org-add-hard-indentation (arg)
  (interactive "p")
  (save-excursion
    (beginning-of-buffer)
    (while
        (let ((indent-size
               (progn
                 (when (re-search-forward "^\\*+ " nil t)
                   (length (match-string 0))))))
          (when indent-size
            (let ((start
                   (progn
                     (next-line)
                     (move-beginning-of-line 1)
                     (point)))
                  (end
                   (progn
                     (if (re-search-forward "^\\*+ " nil t)
                         (progn (previous-line) (move-end-of-line 1))
                       (end-of-buffer))
                     (point))))

              (indent-rigidly start end (* indent-size arg))))
          indent-size))))

(defun org-delete-hard-indentation (arg)
  (interactive "p")
  (org-add-hard-indentation (- arg)))

However, org-delete-hard-indentation only works when hard indentations are consistent. In your example, list items are not indented, but text in the second and third headings are indented, so the example is inconsistent.

In addition, the proposed functions are not sophisticated. Therefore, do not use org-add-hard-indentation / org-delete-hard-indentation when hard indentations do / don't exist already.

1

Org mode already has functions to remove or add hard indentations. org-unindent-buffer removes hard indentations in a buffer. org-indent-region adds hard indentations in a selected region when org-indent-mode is disabled.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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