help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using comment characters for specific major modes


From: Stefan Monnier
Subject: Re: Using comment characters for specific major modes
Date: Sat, 05 Jun 2021 23:24:13 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

> That uses the command (comment-dwim ARG), but I got to get the comment 
> characters
> to append to the string "s".
>
>   (setq-local s (concat ";; " (make-string lena ?\;)))
>
> At least that was my plan.  You may know the proper way to do it though.

Here's how you could go about finding the answer:

Seeing that `comment-dwim` manages to find the ";;" you're looking for,
you could look into it code: `C-h o comment-dwim RET` then click on the
link to jump to the definition.  There you'll see:

    [...]
    (comment-normalize-vars)
    (if (use-region-p)
        (comment-or-uncomment-region (region-beginning) (region-end) arg)
      (if (save-excursion (beginning-of-line) (not (looking-at "\\s-*$")))
          ;; FIXME: If there's no comment to kill on this line and ARG is
          ;; specified, calling comment-kill is not very clever.
          (if arg (comment-kill (and (integerp arg) arg)) (comment-indent))
        ;; Inserting a comment on a blank line. comment-indent calls
        ;; c-i-c-f if needed in the non-blank case.
        (if comment-insert-comment-function
            (funcall comment-insert-comment-function)
          (let ((add (comment-add arg)))
            ;; Some modes insist on keeping column 0 comment in column 0
            ;; so we need to move away from it before inserting the comment.
            (indent-according-to-mode)
            (insert (comment-padright comment-start add))
            (save-excursion
              (unless (string= "" comment-end)
                (insert (comment-padleft comment-end add)))
              (indent-according-to-mode)))))))

And here you already see some functions and variable which might give
you some answers, such as `comment-add`,
`comment-insert-comment-function`, `comment-start`, ...
Using `C-h o` on those should fairly quickly lead you to:

    comment-start is a variable defined in ‘newcomment.el’.
    Its value is ";"
    Local in buffer newcomment.el.gz; global value is nil
    
      This variable is safe as a file local variable if its value
      satisfies the predicate ‘string-or-null-p’.
      Probably introduced at or before Emacs version 21.1.
    
    Documentation:
    String to insert to start a new comment, or nil if no comment syntax.
    
    [back]


-- Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]