Function: indent-region

indent-region is an interactive and byte-compiled function defined in indent.el.gz.

Signature

(indent-region START END &optional COLUMN)

Documentation

Indent each nonblank line in the region.

A numeric prefix argument specifies a column: indent each line to that column.

With no prefix argument, the command chooses one of these methods and indents all the lines with it:

  1) If fill-prefix is non-nil, insert fill-prefix at the
     beginning of each line in the region that does not already begin
     with it.
  2) If indent-region-function is non-nil, call that function
     to indent the region.
  3) Indent each line via indent-according-to-mode.

Called from a program, START and END specify the region to indent. If the third argument COLUMN is an integer, it specifies the column to indent to; if it is nil, use one of the three methods above.

View in manual

Key Bindings

Aliases

vhdl-indent-region (obsolete since 28.1)

Source Code

;; Defined in /usr/src/emacs/lisp/indent.el.gz
(defun indent-region (start end &optional column)
  "Indent each nonblank line in the region.
A numeric prefix argument specifies a column: indent each line to that column.

With no prefix argument, the command chooses one of these methods and
indents all the lines with it:

  1) If `fill-prefix' is non-nil, insert `fill-prefix' at the
     beginning of each line in the region that does not already begin
     with it.
  2) If `indent-region-function' is non-nil, call that function
     to indent the region.
  3) Indent each line via `indent-according-to-mode'.

Called from a program, START and END specify the region to indent.
If the third argument COLUMN is an integer, it specifies the
column to indent to; if it is nil, use one of the three methods above."
  (interactive "r\nP")
  (cond
   ;; If a numeric prefix is given, indent to that column.
   (column
    (setq column (prefix-numeric-value column))
    (save-excursion
      (goto-char end)
      (setq end (point-marker))
      (goto-char start)
      (or (bolp) (forward-line 1))
      (while (< (point) end)
	(delete-region (point) (progn (skip-chars-forward " \t") (point)))
	(or (eolp)
	    (indent-to column 0))
        (forward-line 1))
      (move-marker end nil)))
   ;; If a fill-prefix is specified, use it.
   (fill-prefix
    (save-excursion
      (goto-char end)
      (setq end (point-marker))
      (goto-char start)
      (let ((regexp (regexp-quote fill-prefix)))
	(while (< (point) end)
	  (or (looking-at regexp)
	      (and (bolp) (eolp))
	      (insert fill-prefix))
	  (forward-line 1)))))
   ;; Use indent-region-function is available.
   (indent-region-function
    (save-restriction
      (widen)
      (funcall indent-region-function start end)))
   ;; Else, use a default implementation that calls indent-line-function on
   ;; each line.
   (t
    (save-restriction
      (widen)
      (indent-region-line-by-line start end))))
  ;; In most cases, reindenting modifies the buffer, but it may also
  ;; leave it unmodified, in which case we have to deactivate the mark
  ;; by hand.
  (setq deactivate-mark t))