Function: org-indent-region
org-indent-region is an interactive and byte-compiled function defined
in org.el.gz.
Signature
(org-indent-region START END)
Documentation
Indent each non-blank line in the region.
Called from a program, START and END specify the region to indent. The function will not indent contents of example blocks, verse blocks and export blocks as leading white spaces are assumed to be significant there.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/org/org.el.gz
(defun org-indent-region (start end)
"Indent each non-blank line in the region.
Called from a program, START and END specify the region to
indent. The function will not indent contents of example blocks,
verse blocks and export blocks as leading white spaces are
assumed to be significant there."
(interactive "r")
(save-excursion
(goto-char start)
(skip-chars-forward " \r\t\n")
(unless (eobp) (forward-line 0))
(let ((indent-to
(lambda (ind pos)
;; Set IND as indentation for all lines between point and
;; POS. Blank lines are ignored. Leave point after POS
;; once done.
(let ((limit (copy-marker pos)))
(while (< (point) limit)
(unless (looking-at-p "[ \t]*$") (indent-line-to ind))
(forward-line))
(set-marker limit nil))))
(end (copy-marker end)))
(while (< (point) end)
(if (or (looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
(let* ((element (org-element-at-point))
(type (org-element-type element))
(element-end (copy-marker (org-element-end element)))
(ind (org--get-expected-indentation element nil)))
(cond
;; Element indented as a single block. Example blocks
;; preserving indentation are a special case since the
;; "contents" must not be indented whereas the block
;; boundaries can.
((or (memq type '(export-block latex-environment))
(and (eq type 'example-block)
(not (org-src-preserve-indentation-p element))))
(let ((offset (- ind (current-indentation))))
(unless (zerop offset)
(indent-rigidly (org-element-begin element)
(org-element-end element)
offset)))
(goto-char element-end))
;; Elements indented line wise. Be sure to exclude
;; example blocks (preserving indentation) and source
;; blocks from this category as they are treated
;; specially later.
((or (memq type '(paragraph table table-row))
(not (or (org-element-contents-begin element)
(memq type '(example-block src-block)))))
(when (eq type 'node-property)
(org--align-node-property)
(forward-line 0))
(funcall indent-to ind (min element-end end)))
;; Elements consisting of three parts: before the
;; contents, the contents, and after the contents. The
;; contents are treated specially, according to the
;; element type, or not indented at all. Other parts are
;; indented as a single block.
(t
(let* ((post (copy-marker
(org-element-post-affiliated element)))
(cbeg
(copy-marker
(cond
((not (org-element-contents-begin element))
;; Fake contents for source blocks.
(org-with-wide-buffer
(goto-char post)
(line-beginning-position 2)))
((memq type '(footnote-definition item plain-list))
;; Contents in these elements could start on
;; the same line as the beginning of the
;; element. Make sure we start indenting
;; from the second line.
(org-with-wide-buffer
(goto-char post)
(end-of-line)
(skip-chars-forward " \r\t\n")
(if (eobp) (point) (line-beginning-position))))
(t (org-element-contents-begin element)))))
(cend (copy-marker
(or (org-element-contents-end element)
;; Fake contents for source blocks.
(org-with-wide-buffer
(goto-char element-end)
(skip-chars-backward " \r\t\n")
(line-beginning-position)))
t)))
;; Do not change items indentation individually as it
;; might break the list as a whole. On the other
;; hand, when at a plain list, indent it as a whole.
(cond ((eq type 'plain-list)
(let ((offset (- ind (org-current-text-indentation))))
(unless (zerop offset)
(indent-rigidly (org-element-begin element)
(org-element-end element)
offset))
(goto-char cbeg)))
((eq type 'item) (goto-char cbeg))
(t (funcall indent-to ind (min cbeg end))))
(when (< (point) end)
(cl-case type
((example-block verse-block))
(src-block
;; In a source block, indent source code
;; according to language major mode, but only if
;; `org-src-tab-acts-natively' is non-nil.
(when (and (< (point) end) org-src-tab-acts-natively)
(ignore-errors
(org-babel-do-in-edit-buffer
(indent-region (point-min) (point-max))))))
(t (org-indent-region (point) (min cend end))))
(goto-char (min cend end))
(when (< (point) end)
(funcall indent-to ind (min element-end end))))
(set-marker post nil)
(set-marker cbeg nil)
(set-marker cend nil))))
(set-marker element-end nil))))
(set-marker end nil))))