Subject: bug#70804: 29.3.50; Feature request: Allow setting INITIAL-INPUT in comment-normalize-vars when comment-start is nil

From: Stefan Kangas <[email-masked]>
Date: Sun, 2 Mar 2025 17:59:11 -0800

Daan Ro <[email-masked]> writes:

> "#" is the most common comment syntax in text files whose major mode
> couldn't be recognized automatically.
>
> I hope this can be customized in Emacs 30.

Thanks for the patch, and sorry for the long delay.

Maybe we should just offer "#" as the default instead, if it's the most
common one?  My reasoning here is that there is no need to add a
defcustom, if no one has asked for it.

> From f0a011fdc888f41eb49cb44e1be577261f8b2a9b Mon Sep 17 00:00:00 2001
> From: Daanturo <[email-masked]>
> Date: Mon, 6 May 2024 23:16:56 +0700
> Subject: [PATCH] Add `comment-start-default-suggestion' user option
>
> * lisp/newcomment.el: Define `comment-start-default-suggestion' Lisp
> variable and use it in `comment-normalize-vars'.
>
> * etc/NEWS: Announce the option.
> ---
>  etc/NEWS           | 5 +++++
>  lisp/newcomment.el | 9 ++++++++-
>  2 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/etc/NEWS b/etc/NEWS
> index 456f9b8f8b8..fa9b7c82a97 100644
> --- a/etc/NEWS
> +++ b/etc/NEWS
> @@ -541,6 +541,11 @@ whereas if the mouse pointer is in the left half of a glyph, point
>  will be put in front the buffer position corresponding to that glyph.
>  By default this is disabled.
>
> +---
> +** New user option 'comment-start-default-suggestion'.
> +When 'comment-normalize-vars' asks for nil 'comment-start', use this as
> +the default suggested string in the prompt.
> +
>  ** Internationalization
>
>  ---
> diff --git a/lisp/newcomment.el b/lisp/newcomment.el
> index ee7b2ea34d8..86529668067 100644
> --- a/lisp/newcomment.el
> +++ b/lisp/newcomment.el
> @@ -349,6 +349,12 @@ terminated by the end of line (i.e., `comment-end' is empty)."
>    "Return the mirror image of string S, without any trailing space."
>    (comment-string-strip (concat (nreverse (string-to-list s))) nil t))
>
> +(defcustom comment-start-default-suggestion nil
> +  "The default suggestion when unset `comment-start' is asked."
> +  :type 'string
> +  :version "30.1"
> +  :group 'comment)
> +
>  ;;;###autoload
>  (defun comment-normalize-vars (&optional noerror)
>    "Check and set up variables needed by other commenting functions.
> @@ -358,7 +364,8 @@ functions work correctly.  Lisp callers of any other `comment-*'
>  function should first call this function explicitly."
>    (unless (and (not comment-start) noerror)
>      (unless comment-start
> -      (let ((cs (read-string "No comment syntax is defined.  Use: ")))
> +      (let ((cs (read-string "No comment syntax is defined.  Use: "
> +                          comment-start-default-suggestion)))
>  	(if (zerop (length cs))
>  	    (error "No comment syntax defined")
>            (setq-local comment-start cs)