1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Put the define-minor-mode boilerplate at the end of the doc strings

* lisp/emacs-lisp/easy-mmode.el (easy-mmode--mode-docstring): Put
the boilerplate at the end of the doc string.
This commit is contained in:
Lars Ingebrigtsen 2022-04-02 15:55:29 +02:00
parent 9c30276c42
commit ec464789df

View file

@ -82,9 +82,7 @@ replacing its case-insensitive matches with the literal string in LIGHTER."
(replace-regexp-in-string (regexp-quote lighter) lighter name t t)))) (replace-regexp-in-string (regexp-quote lighter) lighter name t t))))
(defconst easy-mmode--arg-docstring (defconst easy-mmode--arg-docstring
" "This is a minor mode. If called interactively, toggle the `%s'
This is a minor mode. If called interactively, toggle the `%s'
mode. If the prefix argument is positive, enable the mode, and mode. If the prefix argument is positive, enable the mode, and
if it is zero or negative, disable the mode. if it is zero or negative, disable the mode.
@ -100,27 +98,47 @@ it is disabled.")
(defun easy-mmode--mode-docstring (doc mode-pretty-name keymap-sym (defun easy-mmode--mode-docstring (doc mode-pretty-name keymap-sym
getter) getter)
(let ((doc (or doc (format "Toggle %s on or off. ;; If we have a doc string, and it's already complete (which we
;; guess at with the simple heuristic below), then just return that
\\{%s}" mode-pretty-name keymap-sym)))) ;; as is.
(if (string-match-p "\\bARG\\b" doc) (if (and doc (string-match-p "\\bARG\\b" doc))
doc doc
(let* ((fill-prefix nil) ;; Compose a new doc string.
(docs-fc (bound-and-true-p emacs-lisp-docstring-fill-column)) (with-temp-buffer
(fill-column (if (integerp docs-fc) docs-fc 65)) (let ((lines (if doc
(argdoc (format easy-mmode--arg-docstring mode-pretty-name (string-lines doc)
;; Avoid having quotes turn into pretty quotes. (list (format "Toggle %s on or off." mode-pretty-name)))))
(string-replace "'" "\\\\='" ;; Insert the first line from the doc string.
(format "%S" getter)))) (insert (pop lines))
(filled (if (fboundp 'fill-region) ;; Ensure that we have (only) one blank line after the first
(with-temp-buffer ;; line.
(insert argdoc) (ensure-empty-lines)
(fill-region (point-min) (point-max) 'left t) (while (and lines
(buffer-string)) (string-empty-p (car lines)))
argdoc))) (pop lines))
(replace-regexp-in-string "\\(\n\n\\|\\'\\)\\(.\\|\n\\)*\\'" ;; Insert the doc string.
(concat filled "\\1") (dolist (line lines)
doc nil nil 1))))) (insert line "\n"))
(ensure-empty-lines)
;; Insert the boilerplate.
(let* ((fill-prefix nil)
(docs-fc (bound-and-true-p emacs-lisp-docstring-fill-column))
(fill-column (if (integerp docs-fc) docs-fc 65))
(argdoc (format easy-mmode--arg-docstring mode-pretty-name
;; Avoid having quotes turn into pretty quotes.
(string-replace "'" "\\\\='"
(format "%S" getter)))))
(let ((start (point)))
(insert argdoc)
(when (fboundp 'fill-region)
(fill-region start (point) 'left t))))
;; Finally, insert the keymap.
(when (and (boundp keymap-sym)
(or (not doc)
(not (string-search "\\{" doc))))
(ensure-empty-lines)
(insert (format "\\{%s}" keymap-sym)))
(buffer-string)))))
;;;###autoload ;;;###autoload
(defalias 'easy-mmode-define-minor-mode #'define-minor-mode) (defalias 'easy-mmode-define-minor-mode #'define-minor-mode)