New minor mode: `electric-block-comment-mode'

This minor lets you automatically closing block comments after
typing `block-comment-start'.  Thus, typing "/*" in c-mode and
its derivatives automatically inserts "*/".  (Bug#77081)

* etc/NEWS: Add minor-mode item.
* lisp/electric.el
(electric-block-comment-post-self-insert-function): New function.
(electric-block-comment-mode): New minor mode definition.
This commit is contained in:
Elías Gabriel Pérez 2025-03-17 12:56:52 -06:00 committed by Eli Zaretskii
parent 2dd871a358
commit 989f9f01f7
2 changed files with 41 additions and 0 deletions

View file

@ -113,6 +113,17 @@ If you have been using these variables in Lisp code (for example, in
font-lock rules), simply quote the symbol, to use the face directly
instead of its now-obsolete variable.
** New minor mode 'electric-block-comment-mode'
This mode automatically close block comment, typing `block-comment-start'
closes it inserting their corresponding `block-comment-end'.
Thus, allows closing block comments for major modes that support it,
such as:
- c-mode, c++-mode, java-mode, js-mode, css-mode, and derived
- html-mode, mhtml-mode, xml-mode and nxml-mode
- pascal-mode
- lua-ts-mode
- lisp-mode and common-lisp-mode
** Network Security Manager (NSM) is now more strict.
*** NSM warns about TLS 1.1 by default.

View file

@ -731,6 +731,36 @@ use `electric-quote-local-mode'."
(setq-default electric-quote-mode nil) ; But keep it globally disabled.
)))
;;; Electric comment block
(defun electric-block-comment-post-self-insert-function ()
"Function that `electric-block-comment' adds to `post-self-insert-hook'.
This closes block comment with `block-comment-end' when `block-comment-start'
is typed."
(when (and block-comment-start block-comment-end
;; Check if we are exactly behind a `block-comment-start'
(save-excursion
(save-match-data
(re-search-backward (regexp-quote block-comment-start)
(- (point) (length block-comment-start))
t)))
;; And if there is not anything front us
(looking-at-p (concat "[^[:space:]]")))
(insert " ")
(save-excursion
(insert (concat " " block-comment-end)))))
(define-minor-mode electric-block-comment-mode
"Toggle automatic closing of block comments (Electric Block Comment mode).
When enabled, typing `block-comment-start' closes it inserting their
corresponding `block-comment-end'."
:group 'electricity
:version "31.1"
(if electric-block-comment-mode
(add-hook 'post-self-insert-hook #'electric-block-comment-post-self-insert-function 10 t)
(remove-hook 'post-self-insert-hook #'electric-block-comment-post-self-insert-function t)))
(provide 'electric)
;;; electric.el ends here