Function: comment-indent-new-line
comment-indent-new-line is an interactive and byte-compiled function
defined in newcomment.el.gz.
Signature
(comment-indent-new-line &optional SOFT)
Documentation
Break line at point and indent, continuing comment if within one.
This indents the body of the continued comment under the previous comment line.
This command is intended for styles where you write a comment per line,
starting a new comment (and terminating it if necessary) on each line.
If you want to continue one comment across several lines, use M-x newline-and-indent (newline-and-indent).
If a fill column is specified, it overrides the use of the comment column or comment indentation.
The inserted newline is marked hard if variable use-hard-newlines(var)/use-hard-newlines(fun) is true,
unless optional argument SOFT is non-nil.
Key Bindings
Aliases
Source Code
;; Defined in /usr/src/emacs/lisp/newcomment.el.gz
;;;###autoload
(defun comment-indent-new-line (&optional soft)
"Break line at point and indent, continuing comment if within one.
This indents the body of the continued comment
under the previous comment line.
This command is intended for styles where you write a comment per line,
starting a new comment (and terminating it if necessary) on each line.
If you want to continue one comment across several lines, use \\[newline-and-indent].
If a fill column is specified, it overrides the use of the comment column
or comment indentation.
The inserted newline is marked hard if variable `use-hard-newlines' is true,
unless optional argument SOFT is non-nil."
(interactive)
(comment-normalize-vars t)
(let (compos comin)
;; If we are not inside a comment and we only auto-fill comments,
;; don't do anything (unless no comment syntax is defined).
(unless (and comment-start
comment-auto-fill-only-comments
(not (called-interactively-p 'interactive))
(not (save-excursion
(prog1 (setq compos (comment-beginning))
(setq comin (point))))))
;; Now we know we should auto-fill.
;; Insert the newline before removing empty space so that markers
;; get preserved better.
(if soft (insert-and-inherit ?\n) (newline 1))
(save-excursion (forward-char -1) (delete-horizontal-space))
(delete-horizontal-space)
(if (and fill-prefix (not adaptive-fill-mode))
;; Blindly trust a non-adaptive fill-prefix.
(progn
(indent-to-left-margin)
(insert-before-markers-and-inherit fill-prefix))
;; If necessary check whether we're inside a comment.
(unless (or compos (null comment-start))
(save-excursion
(backward-char)
(setq compos (comment-beginning))
(setq comin (point))))
(cond
;; If there's an adaptive prefix, use it unless we're inside
;; a comment and the prefix is not a comment starter.
((and fill-prefix
(comment-valid-prefix-p fill-prefix compos))
(indent-to-left-margin)
(insert-and-inherit fill-prefix))
;; If we're not inside a comment, just try to indent.
((not compos) (indent-according-to-mode))
(t
(let* ((comstart (buffer-substring compos comin))
(normalp
(string-match (regexp-quote (comment-string-strip
comment-start t t))
comstart))
(comend
(if normalp comment-end
;; The comment starter is not the normal comment-start
;; so we can't just use comment-end.
(save-excursion
(goto-char compos)
(if (not (comment-forward)) comment-end
(comment-string-strip
(buffer-substring
(save-excursion (comment-enter-backward) (point))
(point))
nil t))))))
(if (and comment-multi-line (> (length comend) 0))
(indent-according-to-mode)
(insert-and-inherit ?\n)
(forward-char -1)
(let* ((comment-column
;; The continuation indentation should be somewhere
;; between the current line's indentation (plus 2 for
;; good measure) and the current comment's indentation,
;; with a preference for comment-column.
(save-excursion
;; FIXME: use prev line's info rather than first
;; line's.
(goto-char compos)
(min (current-column)
(max comment-column
(+ 2 (current-indentation))))))
(comment-indent-function
;; If the previous comment is on its own line, then
;; reuse its indentation unconditionally.
;; Important for modes like Python/Haskell where
;; auto-indentation is unreliable.
(if (save-excursion (goto-char compos)
(skip-chars-backward " \t")
(bolp))
(lambda () comment-column) comment-indent-function))
(comment-start comstart)
(comment-end comend)
(continuep (or comment-multi-line
(cadr (assoc comment-style
comment-styles))))
;; Recreate comment-continue from comment-start.
;; FIXME: wrong if comment-continue was set explicitly!
;; FIXME: use prev line's continuation if available.
(comment-continue nil))
(comment-indent continuep))
(save-excursion
(let ((pt (point)))
(end-of-line)
(let ((comend (buffer-substring pt (point))))
;; The 1+ is to make sure we delete the \n inserted above.
(delete-region pt (1+ (point)))
(end-of-line 0)
(insert comend))))))))))))