Function: indent-for-tab-command
indent-for-tab-command is an interactive and byte-compiled function
defined in indent.el.gz.
Signature
(indent-for-tab-command &optional ARG)
Documentation
Indent the current line or region, or insert a tab, as appropriate.
This function either inserts a tab, or indents the current line,
or performs symbol completion, depending on tab-always-indent.
The function called to actually indent the line or insert a tab
is given by the variable indent-line-function.
If a prefix argument is given (ARG), after this function indents the current line or inserts a tab, it also rigidly indents the entire balanced expression which starts at the beginning of the current line, to reflect the current line's indentation.
In most major modes, if point was in the current line's indentation, it is moved to the first non-whitespace character after indenting; otherwise it stays at the same position relative to the text.
If transient-mark-mode(var)/transient-mark-mode(fun) is turned on and the region is active,
this function instead calls indent-region. In this case, any
prefix argument is ignored.
Probably introduced at or before Emacs version 24.1.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/indent.el.gz
(defun indent-for-tab-command (&optional arg)
"Indent the current line or region, or insert a tab, as appropriate.
This function either inserts a tab, or indents the current line,
or performs symbol completion, depending on `tab-always-indent'.
The function called to actually indent the line or insert a tab
is given by the variable `indent-line-function'.
If a prefix argument is given (ARG), after this function indents the
current line or inserts a tab, it also rigidly indents the entire
balanced expression which starts at the beginning of the current
line, to reflect the current line's indentation.
In most major modes, if point was in the current line's
indentation, it is moved to the first non-whitespace character
after indenting; otherwise it stays at the same position relative
to the text.
If `transient-mark-mode' is turned on and the region is active,
this function instead calls `indent-region'. In this case, any
prefix argument is ignored."
(interactive "P")
(cond
;; The region is active, indent it.
((use-region-p)
(indent-region (region-beginning) (region-end)))
((or ;; indent-to-left-margin is only meant for indenting,
;; so we force it to always insert a tab here.
(eq indent-line-function 'indent-to-left-margin)
(and (not tab-always-indent)
(or (> (current-column) (current-indentation))
(eq this-command last-command))))
(insert-tab arg))
(t
(let ((old-tick (buffer-chars-modified-tick))
(old-point (point))
(old-indent (current-indentation)))
;; Indent the line.
(or (not (eq (indent--funcall-widened indent-line-function) 'noindent))
(indent--default-inside-comment)
(when (or (<= (current-column) (current-indentation))
(not (eq tab-always-indent 'complete)))
(indent--funcall-widened (default-value 'indent-line-function))))
(cond
;; If the text was already indented right, try completion.
((and (eq tab-always-indent 'complete)
(eql old-point (point))
(eql old-tick (buffer-chars-modified-tick))
(or (eq last-command this-command)
(let ((syn (syntax-class (syntax-after (point)))))
(pcase tab-first-completion
('nil t)
('eol (eolp))
('word (not (eql 2 syn)))
('word-or-paren (not (memq syn '(2 4 5))))
('word-or-paren-or-punct (not (memq syn '(2 4 5 1))))))))
(completion-at-point))
;; If a prefix argument was given, rigidly indent the following
;; sexp to match the change in the current line's indentation.
(arg
(let ((end-marker
(save-excursion
(forward-line 0) (forward-sexp) (point-marker)))
(indentation-change (- (current-indentation) old-indent)))
(save-excursion
(forward-line 1)
(when (and (not (zerop indentation-change))
(< (point) end-marker))
(indent-rigidly (point) end-marker indentation-change))))))))))