By default, comments get an indentation level which seems rather alien to me.
(defun example ()
just
some
; a comment
words)
How to adjust it so the first semicolon is vertically aligned with the regular Lisp forms?
(defun example ()
just
some
; a comment
words)
What I could find out is that the default mechanism works by aligning the comments to a fixed column (queryable via M-x comment-set-column
), and that one can modify the comment-indent-function
variable (setting it to nil partially fixes my problem).
Emacs indents comments in elisp differently depending on the number of semicolons used. If you use two, you should get the indentation you're after:
(defun test-single ()
; A single semicolon
nil)
(defun test-double ()
;; Do two semicolons make a colon ;)
nil)
In addition, three semicolons ;;;
are not re-indented at all. Typically, they are used to mark new major sections in the source file.
You may customize comment-indent-function
Instead of comment-indent-default use your own function.
Writing a new by replacing in last line `comment-column' by (save-excursion (forward-line -1)(current-indentation))
should deliver a starting point.
If you remove the case for single semicolon comments from lisp-indent-line
it will behave as you want to.
I have removed it in the code below, you can add this to your emacs config:
(defun lisp-indent-line (&optional _whole-exp)
"Indent current line as Lisp code.
With argument, indent any additional lines of the same expression
rigidly along with this one.
Modified to indent single semicolon comments like double semicolon comments"
(interactive "P")
(let ((indent (calculate-lisp-indent)) shift-amt
(pos (- (point-max) (point)))
(beg (progn (beginning-of-line) (point))))
(skip-chars-forward " \t")
(if (or (null indent) (looking-at "\\s<\\s<\\s<"))
;; Don't alter indentation of a ;;; comment line
;; or a line that starts in a string.
;; FIXME: inconsistency: comment-indent moves ;;; to column 0.
(goto-char (- (point-max) pos))
(if (listp indent) (setq indent (car indent)))
(setq shift-amt (- indent (current-column)))
(if (zerop shift-amt)
nil
(delete-region beg (point))
(indent-to indent))
;; If initial point was within line's indentation,
;; position after the indentation. Else stay at same point in text.
(if (> (- (point-max) pos) (point))
(goto-char (- (point-max) pos))))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With