Re: Dynamic comment prefix generator

tpeplt <[email protected]> Wed, 01 Jul 2026 10:48:31 -0400
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Heime <[email protected]> writes:

> This code constructs a list of pairs, where each pair consists 
> of a formatted string prefix and a corresponding number. For 
> example, (klevels ";;;" 2) produces ((";;; <1>" . 1) (";;; <2>" . 2)
>
> (defun klevels (prefix n)
>   (cl-loop for i from 1 to n
>             collect (cons (format "%s %s%d%s" prefix "<" i ">") i)))
>
> To make the prefix dynamically based on the major mode's comment 
> character.  For single character comment strings, I want to repeat 
> it three times.
> comment declarations. 
>

It sounds like you want to use the mode-specific values of
‘comment-start’ and ‘comment-end’, whose values are strings.
(These are defined in Emacs library newcomment.el.)

For example, you might try evaluating the following
expression for buffers in several of the modes that you are
interested in using:

   M-: (klevels (make-string 3 (string-to-char comment-start)) 3)

or
   (let ((my-prefix (make-string 3 (string-to-char comment-start))))
     (klevels my-prefix 3))

In the *scratch* buffer, this results in:

   ((";;; <1>" . 1) (";;; <2>" . 2) (";;; <3>" . 3))

Note that if you are working with buffers for .c files, then
this will not work because the solution assumes that the
comment-start is a single character, where for .c it is two
characters "/*" (and there is a comment end string "*/").

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.