various major modes. The function is called with no arguments, but with
point at the beginning of the comment, or at the end of a line if a new
comment is to be inserted. It should return the column in which the
-comment ought to start. For example, in Lisp mode, the indent hook
-function bases its decision on how many semicolons begin an existing
-comment, and on the code in the preceding lines.
+comment ought to start. For example, the default hook function bases
+its decision on how many comment characters begin an existing comment.
+
+Emacs also tries to align comments on adjacent lines. To override
+this, the function may return a cons of two (possibly equal) integers
+to indicate an acceptable range of indentation.
@node Documentation
@section Documentation Lookup
;;;###autoload
(defvar comment-indent-function 'comment-indent-default
"Function to compute desired indentation for a comment.
-This function is called with no args with point at the beginning of
-the comment's starting delimiter and should return either the desired
-column indentation or nil.
+This function is called with no args with point at the beginning
+of the comment's starting delimiter and should return either the
+desired column indentation, a range of acceptable
+indentation (MIN . MAX), or nil.
If nil is returned, indentation is delegated to `indent-according-to-mode'.")
;;;###autoload
- prefer INDENT (or `comment-column' if nil).
Point is expected to be at the start of the comment."
(unless indent (setq indent comment-column))
- ;; Avoid moving comments past the fill-column.
- (let ((max (+ (current-column)
- (- (or comment-fill-column fill-column)
- (save-excursion (end-of-line) (current-column)))))
- (other nil)
- (min (save-excursion (skip-chars-backward " \t")
- (if (bolp) 0 (+ comment-inline-offset (current-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 the comment is at the right of code, adjust the indentation.
(unless (save-excursion (skip-chars-backward " \t") (bolp))
(setq indent (comment-choose-indent indent)))
- ;; Update INDENT to leave at least one space
- ;; after other nonwhite text on the line.
- (save-excursion
- (skip-chars-backward " \t")
- (unless (bolp)
- (setq indent (max indent
- (+ (current-column) comment-inline-offset)))))
;; If that's different from comment's current position, change it.
(unless (= (current-column) indent)
(delete-region (point) (progn (skip-chars-backward " \t") (point)))