32.17.3 Indenting an Entire Region
This section describes commands that indent all the lines in the region. They return unpredictable values.
command
indent-region start end \&optional to-column​
This command indents each nonblank line starting between start
(inclusive) and end
(exclusive). If to-column
is nil
, indent-region
indents each nonblank line by calling the current mode’s indentation function, the value of indent-line-function
.
If to-column
is non-nil
, it should be an integer specifying the number of columns of indentation; then this function gives each line exactly that much indentation, by either adding or deleting whitespace.
If there is a fill prefix, indent-region
indents each line by making it start with the fill prefix.
variable
indent-region-function​
The value of this variable is a function that can be used by indent-region
as a short cut. It should take two arguments, the start and end of the region. You should design the function so that it will produce the same results as indenting the lines of the region one by one, but presumably faster.
If the value is nil
, there is no short cut, and indent-region
actually works line by line.
A short-cut function is useful in modes such as C mode and Lisp mode, where the indent-line-function
must scan from the beginning of the function definition: applying it to each line would be quadratic in time. The short cut can update the scan information as it moves through the lines indenting them; this takes linear time. In a mode where indenting a line individually is fast, there is no need for a short cut.
indent-region
with a non-nil
argument to-column
has a different meaning and does not use this variable.
command
indent-rigidly start end count​
This function indents all lines starting between start
(inclusive) and end
(exclusive) sideways by count
columns. This preserves the shape of the affected region, moving it as a rigid unit.
This is useful not only for indenting regions of unindented text, but also for indenting regions of formatted code. For example, if count
is 3, this command adds 3 columns of indentation to every line that begins in the specified region.
If called interactively with no prefix argument, this command invokes a transient mode for adjusting indentation rigidly. See Indentation Commands in The GNU Emacs Manual.
command
indent-code-rigidly start end columns \&optional nochange-regexp​
This is like indent-rigidly
, except that it doesn’t alter lines that start within strings or comments.
In addition, it doesn’t alter a line if nochange-regexp
matches at the beginning of the line (if nochange-regexp
is non-nil
).