501 |
(goto-char begpos) |
(goto-char begpos) |
502 |
;; Compute desired indent. |
;; Compute desired indent. |
503 |
(setq indent (save-excursion (funcall comment-indent-function))) |
(setq indent (save-excursion (funcall comment-indent-function))) |
504 |
|
;; If `indent' is nil and there's code before the comment, we can't |
505 |
|
;; use `indent-according-to-mode', so we default to comment-column. |
506 |
|
(unless (or indent (save-excursion (skip-chars-backward " \t") (bolp))) |
507 |
|
(setq indent comment-column)) |
508 |
(if (not indent) |
(if (not indent) |
509 |
;; comment-indent-function refuses: delegate to line-indent. |
;; comment-indent-function refuses: delegate to line-indent. |
510 |
(indent-according-to-mode) |
(indent-according-to-mode) |
511 |
;; Avoid moving comments past the fill-column. |
;; If the comment is at the left of code, adjust the indentation. |
512 |
(unless (save-excursion (skip-chars-backward " \t") (bolp)) |
(unless (save-excursion (skip-chars-backward " \t") (bolp)) |
513 |
|
;; Avoid moving comments past the fill-column. |
514 |
(let ((max (+ (current-column) |
(let ((max (+ (current-column) |
515 |
(- (or comment-fill-column fill-column) |
(- (or comment-fill-column fill-column) |
516 |
(save-excursion (end-of-line) (current-column)))))) |
(save-excursion (end-of-line) (current-column)))))) |
518 |
(setq indent max) ;Don't move past the fill column. |
(setq indent max) ;Don't move past the fill column. |
519 |
;; We can choose anywhere between indent..max. |
;; We can choose anywhere between indent..max. |
520 |
;; Let's try to align to a comment on the previous line. |
;; Let's try to align to a comment on the previous line. |
521 |
(let ((other nil)) |
(let ((other nil) |
522 |
|
(min (max indent |
523 |
|
(save-excursion (skip-chars-backward " \t") |
524 |
|
(1+ (current-column)))))) |
525 |
(save-excursion |
(save-excursion |
526 |
(when (and (zerop (forward-line -1)) |
(when (and (zerop (forward-line -1)) |
527 |
(setq other (comment-search-forward |
(setq other (comment-search-forward |
528 |
(line-end-position) t))) |
(line-end-position) t))) |
529 |
(goto-char other) (setq other (current-column)))) |
(goto-char other) (setq other (current-column)))) |
530 |
(if (and other (<= other max) (> other indent)) |
(if (and other (<= other max) (>= other min)) |
531 |
;; There is a comment and it's in the range: bingo. |
;; There is a comment and it's in the range: bingo. |
532 |
(setq indent other) |
(setq indent other) |
533 |
;; Let's try to align to a comment on the next line, then. |
;; Let's try to align to a comment on the next line, then. |
537 |
(setq other (comment-search-forward |
(setq other (comment-search-forward |
538 |
(line-end-position) t))) |
(line-end-position) t))) |
539 |
(goto-char other) (setq other (current-column)))) |
(goto-char other) (setq other (current-column)))) |
540 |
(if (and other (<= other max) (> other indent)) |
(if (and other (<= other max) (> other min)) |
541 |
;; There is a comment and it's in the range: bingo. |
;; There is a comment and it's in the range: bingo. |
542 |
(setq indent other)))))))) |
(setq indent other)))))))) |
543 |
(unless (= (current-column) indent) |
(unless (= (current-column) indent) |