Subject: Re: bug#77628: [PATCH] ; Fix `elisp-eldoc-funcall-with-docstring' when fn has no docstring.

From: Elijah Gabe Pérez <[email-masked]>
Date: Sat, 12 Apr 2025 12:23:06 -0600

Eli Zaretskii <[email-masked]> writes:

>> From: Elijah Gabe Pérez <[email-masked]>
>> Date: Mon, 07 Apr 2025 23:08:18 -0600
>> 
>> * lisp/progmodes/elisp-mode.el (elisp-eldoc-funcall-with-docstring):
>> Add "Undocumented" as docstring if the function as no docstring,
>> and limit docstring length to 1000 characters.  (bug#)
>
> Thanks, but should 1000 be hard-coded, or should we perhaps have a
> variable for it?

I've added a variable for limit both
`elisp-eldoc-funcall-with-docstring' and
`elisp-eldoc-var-docstring-with-value'.

`elisp-eldoc-var-docstring-with-value' also set a limit.

>From e9dac14448717cc60f840874147d9097d33f9902 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?El=C3=ADas=20Gabriel=20P=C3=A9rez?= <[email-masked]>
Date: Mon, 7 Apr 2025 23:00:20 -0600
Subject: [PATCH] Add variable for control docstring length in elisp eldoc
 functions

* etc/NEWS: Announce new variable.
* lisp/progmodes/elisp-mode.el (elisp-eldoc-docstring-length-limit):
New user option.
(elisp-eldoc-funcall-with-docstring):
Add "Undocumented" as docstring if the function has no docstring.
(elisp-eldoc-var-docstring-with-value): Use
`elisp-eldoc-docstring-length-limit'.  (bug#77628)
---
 etc/NEWS                     |  7 ++++++
 lisp/progmodes/elisp-mode.el | 41 +++++++++++++++++++++++-------------
 2 files changed, 33 insertions(+), 15 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index c08b0052639..5a9425054bd 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -585,6 +585,13 @@ displayed in 'elisp-eldoc-funcall-with-docstring'.  If set to 'short'
 (the default), only display the first sentence of the doc string.
 Otherwise, if set to 'full', display the full doc string.'
 
+---
+*** New user option 'elisp-eldoc-docstring-length-limit'.
+This user option controls maximum docstring character limit displayed by
+'elisp-eldoc-funcall-with-docstring' and
+'elisp-eldoc-var-docstring-with-value'.  By default it is set to 1000
+characters
+
 ---
 ** Buffer Menu
 
diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index 748d2f11360..bcd6d02ae8f 100644
--- a/lisp/progmodes/elisp-mode.el
+++ b/lisp/progmodes/elisp-mode.el
@@ -1845,6 +1845,12 @@ elisp-eldoc-funcall
                          'font-lock-function-name-face
                        'font-lock-keyword-face)))))
 
+(defcustom elisp-eldoc-docstring-length-limit 1000
+  "Maximum docstring character limit displayed by elisp eldoc functions."
+  :type 'natnum
+  :group 'elisp
+  :version "31.1")
+
 (defcustom elisp-eldoc-funcall-with-docstring-length 'short
   "Control length of doc string shown by `elisp-eldoc-funcall-with-docstring'.
 If set to `short', only show the first sentence of the doc string.
@@ -1861,21 +1867,26 @@ elisp-eldoc-funcall-with-docstring
 Compared to `elisp-eldoc-funcall', this also includes the
 current function doc string, doc string length depends on
 `elisp-eldoc-funcall-with-docstring-length'."
-  (let* ((sym-info (elisp--fnsym-in-current-sexp))
-         (fn-sym (car sym-info))
-         (doc (when (fboundp fn-sym)
-                (propertize
-                 (cdr (help-split-fundoc
-                       (condition-case nil (documentation fn-sym t)
-                         (invalid-function nil))
-                       fn-sym))
-                 'face 'font-lock-doc-face))))
-    (when fn-sym
-      (funcall callback
+  (when-let* ((sym-info (elisp--fnsym-in-current-sexp))
+              (fn-sym (car sym-info))
+              ((fboundp fn-sym))
+              (fn-doc (or (cdr (help-split-fundoc
+                                (condition-case nil (documentation fn-sym t)
+                                  (invalid-function nil))
+                                fn-sym))
+                          "Undocumented."))
+              (more (- (length fn-doc) elisp-eldoc-docstring-length-limit))
+              (doc (concat
+                      (propertize
+                       (string-limit fn-doc elisp-eldoc-docstring-length-limit)
+                       'face 'font-lock-doc-face)
+                      (when (> more 0)
+                        (format "[%sc more]" more)))))
+    (funcall callback
                (concat (apply #'elisp-get-fnsym-args-string sym-info)
                        ;; Ensure not display the docstring in the
                        ;; mode-line.
-                       (when (and doc (not (minibufferp)))
+                       (when (not (minibufferp))
                          (concat
                           "\n"
                           (pcase elisp-eldoc-funcall-with-docstring-length
@@ -1887,7 +1898,7 @@ elisp-eldoc-funcall-with-docstring
                :thing fn-sym
                :face (if (functionp fn-sym)
                          'font-lock-function-name-face
-                       'font-lock-keyword-face)))))
+                       'font-lock-keyword-face))))
 
 (defun elisp-eldoc-var-docstring (callback &rest _ignored)
   "Document variable at point by calling CALLBACK.
@@ -1915,12 +1926,12 @@ elisp-eldoc-var-docstring-with-value
 		       (symbol-value cs)
 		       (let* ((doc (documentation-property
                                     cs 'variable-documentation t))
-			      (more (- (length doc) 1000)))
+			      (more (- (length doc) elisp-eldoc-docstring-length-limit)))
 			 (concat (propertize
 				  (string-limit (if (string= doc "nil")
 						    "Undocumented."
 						  doc)
-					        1000)
+					        elisp-eldoc-docstring-length-limit)
 				  'face 'font-lock-doc-face)
 				 (when (> more 0)
 				   (format "[%sc more]" more)))))
-- 
2.49.0

-- 
                                          - E.G via GNU Emacs and Org.