summaryrefslogtreecommitdiff
path: root/lisp/electric.el
diff options
context:
space:
mode:
authorElías Gabriel Pérez <eg642616@gmail.com>2025-03-17 12:56:52 -0600
committerEli Zaretskii <eliz@gnu.org>2025-03-29 13:57:53 +0300
commit989f9f01f731c0dd0382bad50f1c45894d69c3ea (patch)
tree788d5821a767cceaa0db97f1efd637c235de907b /lisp/electric.el
parent2dd871a358e5aeea7ea343e06497760ac36464cc (diff)
downloademacs-989f9f01f731c0dd0382bad50f1c45894d69c3ea.tar.gz
emacs-989f9f01f731c0dd0382bad50f1c45894d69c3ea.tar.bz2
emacs-989f9f01f731c0dd0382bad50f1c45894d69c3ea.zip
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.
Diffstat (limited to 'lisp/electric.el')
-rw-r--r--lisp/electric.el30
1 files changed, 30 insertions, 0 deletions
diff --git a/lisp/electric.el b/lisp/electric.el
index 39e13e1ca0c..86c01438e9a 100644
--- a/lisp/electric.el
+++ b/lisp/electric.el
@@ -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