Function: comment-choose-indent

comment-choose-indent is a byte-compiled function defined in newcomment.el.gz.

Signature

(comment-choose-indent &optional INDENT)

Documentation

Choose the indentation to use for a right-hand-side comment.

The criteria are (in this order):
- try to keep the comment's text within comment-fill-column.
- try to align with surrounding comments.
- prefer INDENT (or comment-column if nil).
Point is expected to be at the start of the comment.

Source Code

;; Defined in /usr/src/emacs/lisp/newcomment.el.gz
(defun comment-choose-indent (&optional indent)
  "Choose the indentation to use for a right-hand-side comment.
The criteria are (in this order):
- try to keep the comment's text within `comment-fill-column'.
- try to align with surrounding comments.
- prefer INDENT (or `comment-column' if nil).
Point is expected to be at the start of the comment."
  (unless indent (setq indent comment-column))
  (let ((other nil)
        min max)
    (pcase indent
      (`(,lo . ,hi) (setq min lo) (setq max hi)
       (setq indent comment-column))
      (_ ;; Avoid moving comments past the fill-column.
       (setq max (+ (current-column)
                    (- (or comment-fill-column fill-column)
                       (save-excursion (end-of-line) (current-column)))))
       (setq min (save-excursion
                   (skip-chars-backward " \t")
                   ;; Leave at least `comment-inline-offset' space after
                   ;; other nonwhite text on the line.
                   (if (bolp) 0 (+ comment-inline-offset (current-column)))))))
    ;; Fix up the range.
    (if (< max min) (setq max min))
    ;; Don't move past the fill column.
    (if (<= max indent) (setq indent max))
    ;; We can choose anywhere between min..max.
    ;; Let's try to align to a comment on the previous line.
    (save-excursion
      (when (and (zerop (forward-line -1))
                 (setq other (comment-search-forward
                              (line-end-position) t)))
        (goto-char other) (setq other (current-column))))
    (if (and other (<= other max) (>= other min))
        ;; There is a comment and it's in the range: bingo!
        other
      ;; Can't align to a previous comment: let's try to align to comments
      ;; on the following lines, then.  These have not been re-indented yet,
      ;; so we can't directly align ourselves with them.  All we do is to try
      ;; and choose an indentation point with which they will be able to
      ;; align themselves.
      (save-excursion
        (while (and (zerop (forward-line 1))
                    (setq other (comment-search-forward
                                 (line-end-position) t)))
          (goto-char other)
          (let ((omax (+ (current-column)
                         (- (or comment-fill-column fill-column)
                            (save-excursion (end-of-line) (current-column)))))
                (omin (save-excursion (skip-chars-backward " \t")
                                      (1+ (current-column)))))
            (if (and (>= omax min) (<= omin max))
                (progn (setq min (max omin min))
                       (setq max (min omax max)))
              ;; Can't align with this anyway, so exit the loop.
              (goto-char (point-max))))))
      ;; Return the closest point to indent within min..max.
      (max min (min max indent)))))