0

I moved to column 60 of the first line and evaluated set-comment-column. Then I moved to the second column and called indent-for-comment, but it indents the comment randomly but not at column 60.

How to indent all "end of line comments" at column 60?

2

Your question

How to indent all "end of line comments" at column 60?

actually has two parts.

  1. How to indent at column 60.
  2. How to indent all end of line comments

1. How to indent at column 60

The command comment-choose-indent is used for the calculation of the indentation. Its doc-string says:

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.

You are hitting the first item. The comment is most probably overflowing fill-column, the default for comment-fill-column.

Enlarge fill-column sufficiently such that no comment hits that limit.

2. How to indent all end of line comments

You can make indent-for-comment aware of an active region and apply that command to each line in the region with the following advice.

(defun apply-cmd-to-region-lines (cmd &rest args)
  "Apply CMD with ARGS to region lines if region is active.
Just call (apply CMD ARGS) otherwise."
  (if (use-region-p)
      (cl-letf (((symbol-function 'execute-kbd-macro)
         `(lambda (&rest _ignore)
            (interactive)
            (,cmd ,@args))))
    (apply-macro-to-region-lines (region-beginning) (region-end) 'ignore))
    (apply cmd args)))

(advice-add 'comment-indent :around #'apply-cmd-to-region-lines)
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.