aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPo Lu2023-03-12 19:37:00 +0800
committerPo Lu2023-03-12 19:37:00 +0800
commit90c22cb94965efff001968b9e84239c08cf14869 (patch)
treefc695e7c73d41e1211a3a49b8a9cbd6aa8e094cc
parent776f1ca3e3551b98569ab7daa58cd6921048881e (diff)
parent9191fd50d242cde5256b876dd756ffbc6f46db90 (diff)
downloademacs-90c22cb94965efff001968b9e84239c08cf14869.tar.gz
emacs-90c22cb94965efff001968b9e84239c08cf14869.zip
Merge remote-tracking branch 'origin/master' into feature/android
-rw-r--r--doc/emacs/help.texi10
-rw-r--r--doc/lispref/help.texi26
-rw-r--r--etc/NEWS18
-rw-r--r--lisp/emacs-lisp/shortdoc.el123
-rw-r--r--test/lisp/emacs-lisp/shortdoc-tests.el10
-rw-r--r--test/lisp/eshell/em-cmpl-tests.el2
6 files changed, 152 insertions, 37 deletions
diff --git a/doc/emacs/help.texi b/doc/emacs/help.texi
index 2513e6be271..945e12a05d2 100644
--- a/doc/emacs/help.texi
+++ b/doc/emacs/help.texi
@@ -317,6 +317,16 @@ by using the @kbd{M-x shortdoc} command. This will prompt you for an
317area of interest, e.g., @code{string}, and pop you to a buffer where 317area of interest, e.g., @code{string}, and pop you to a buffer where
318many of the functions relevant for handling strings are listed. 318many of the functions relevant for handling strings are listed.
319 319
320You can also request that documentation of functions and commands
321shown in @file{*Help*} buffers popped by @kbd{C-h f} includes examples
322of their use. To that end, add the following to your initialization
323file (@pxref{Init File}):
324
325@example
326(add-hook 'help-fns-describe-function-functions
327 #'shortdoc-help-fns-examples-function)
328@end example
329
320@kindex C-h v 330@kindex C-h v
321@findex describe-variable 331@findex describe-variable
322 @kbd{C-h v} (@code{describe-variable}) is like @kbd{C-h f} but 332 @kbd{C-h v} (@code{describe-variable}) is like @kbd{C-h f} but
diff --git a/doc/lispref/help.texi b/doc/lispref/help.texi
index 59b6b6dab1d..d5e4e1c31d3 100644
--- a/doc/lispref/help.texi
+++ b/doc/lispref/help.texi
@@ -989,3 +989,29 @@ in the function group to insert the function into.
989If @var{group} doesn't exist, it will be created. If @var{section} 989If @var{group} doesn't exist, it will be created. If @var{section}
990doesn't exist, it will be added to the end of the function group. 990doesn't exist, it will be added to the end of the function group.
991@end defun 991@end defun
992
993You can also query the examples of use of functions defined in
994shortdoc groups.
995
996@defun shortdoc-function-examples function
997This function returns all shortdoc examples for @var{function}. The
998return value is an alist with items of the form
999@w{@code{(@var{group} . @var{examples})}}, where @var{group} is a
1000documentation group where @var{function} appears, and @var{examples}
1001is a string with the examples of @var{function}s use as defined in
1002@var{group}.
1003
1004@code{shortdoc-function-examples} returns @code{nil} if @var{function}
1005is not a function or if it doesn't have any shortdoc examples.
1006@end defun
1007
1008@vindex help-fns-describe-function-functions
1009@defun shortdoc-help-fns-examples-function function
1010This function queries the registered shortdoc groups and inserts
1011examples of use of a given Emacs Lisp @var{function} into the current
1012buffer. It is suitable for addition to the
1013@code{help-fns-describe-function-functions} hook, in which case
1014examples from shortdoc of using a function will be displayed in the
1015@file{*Help*} buffer when the documentation of the function is
1016requested.
1017@end defun
diff --git a/etc/NEWS b/etc/NEWS
index 64cf11151b8..716376e1d99 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -232,6 +232,24 @@ asynchronously (which is the default behavior).
232*** New face 'doc-view-svg-face'. 232*** New face 'doc-view-svg-face'.
233This replaces 'doc-view-svg-foreground' and 'doc-view-svg-background'. 233This replaces 'doc-view-svg-foreground' and 'doc-view-svg-background'.
234 234
235** Shortdoc
236
237+++
238*** New function 'shortdoc-function-examples'.
239This function returns examples of use of a given Emacs Lisp function
240from the available shortdoc information.
241
242+++
243*** New function 'shortdoc-help-fns-examples-function'.
244This function inserts into the current buffer examples of use of a
245given Emacs Lisp function, which it gleans from the shortdoc
246information. If you want 'describe-function' ('C-h f') to insert
247examples of using the function into regular *Help* buffers, add the
248following to you init file:
249
250 (add-hook 'help-fns-describe-function-functions
251 #'shortdoc-help-fns-examples-function)
252
235 253
236* New Modes and Packages in Emacs 30.1 254* New Modes and Packages in Emacs 30.1
237 255
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el
index c49960c2ee6..6e3ebc7c6a2 100644
--- a/lisp/emacs-lisp/shortdoc.el
+++ b/lisp/emacs-lisp/shortdoc.el
@@ -1443,45 +1443,52 @@ If SAME-WINDOW, don't pop to a new window."
1443 (setq group (intern group))) 1443 (setq group (intern group)))
1444 (unless (assq group shortdoc--groups) 1444 (unless (assq group shortdoc--groups)
1445 (error "No such documentation group %s" group)) 1445 (error "No such documentation group %s" group))
1446 (funcall (if same-window 1446 (let ((buf (get-buffer-create (format "*Shortdoc %s*" group))))
1447 #'pop-to-buffer-same-window 1447 (shortdoc--insert-group-in-buffer group buf)
1448 #'pop-to-buffer) 1448 (funcall (if same-window
1449 (format "*Shortdoc %s*" group)) 1449 #'pop-to-buffer-same-window
1450 (let ((inhibit-read-only t) 1450 #'pop-to-buffer)
1451 (prev nil)) 1451 buf))
1452 (erase-buffer)
1453 (shortdoc-mode)
1454 (button-mode)
1455 (mapc
1456 (lambda (data)
1457 (cond
1458 ((stringp data)
1459 (setq prev nil)
1460 (unless (bobp)
1461 (insert "\n"))
1462 (insert (propertize
1463 (substitute-command-keys data)
1464 'face 'shortdoc-heading
1465 'shortdoc-section t
1466 'outline-level 1))
1467 (insert (propertize
1468 "\n\n"
1469 'face 'shortdoc-heading
1470 'shortdoc-section t)))
1471 ;; There may be functions not yet defined in the data.
1472 ((fboundp (car data))
1473 (when prev
1474 (insert (make-separator-line)
1475 ;; This helps with hidden outlines (bug#53981)
1476 (propertize "\n" 'face '(:height 0))))
1477 (setq prev t)
1478 (shortdoc--display-function data))))
1479 (cdr (assq group shortdoc--groups))))
1480 (goto-char (point-min)) 1452 (goto-char (point-min))
1481 (when function 1453 (when function
1482 (text-property-search-forward 'shortdoc-function function t) 1454 (text-property-search-forward 'shortdoc-function function t)
1483 (beginning-of-line))) 1455 (beginning-of-line)))
1484 1456
1457(defun shortdoc--insert-group-in-buffer (group &optional buf)
1458 "Insert a short documentation summary for functions in GROUP in buffer BUF.
1459BUF defaults to the current buffer if nil or omitted."
1460 (with-current-buffer (or buf (current-buffer))
1461 (let ((inhibit-read-only t)
1462 (prev nil))
1463 (erase-buffer)
1464 (shortdoc-mode)
1465 (button-mode)
1466 (mapc
1467 (lambda (data)
1468 (cond
1469 ((stringp data)
1470 (setq prev nil)
1471 (unless (bobp)
1472 (insert "\n"))
1473 (insert (propertize
1474 (substitute-command-keys data)
1475 'face 'shortdoc-heading
1476 'shortdoc-section t
1477 'outline-level 1))
1478 (insert (propertize
1479 "\n\n"
1480 'face 'shortdoc-heading
1481 'shortdoc-section t)))
1482 ;; There may be functions not yet defined in the data.
1483 ((fboundp (car data))
1484 (when prev
1485 (insert (make-separator-line)
1486 ;; This helps with hidden outlines (bug#53981)
1487 (propertize "\n" 'face '(:height 0))))
1488 (setq prev t)
1489 (shortdoc--display-function data))))
1490 (cdr (assq group shortdoc--groups))))))
1491
1485;;;###autoload 1492;;;###autoload
1486(defalias 'shortdoc #'shortdoc-display-group) 1493(defalias 'shortdoc #'shortdoc-display-group)
1487 1494
@@ -1521,7 +1528,8 @@ function's documentation in the Info manual"))
1521 "=>")) 1528 "=>"))
1522 (single-arrow (if (char-displayable-p ?→) 1529 (single-arrow (if (char-displayable-p ?→)
1523 "→" 1530 "→"
1524 "->"))) 1531 "->"))
1532 (start-example (point)))
1525 (cl-loop for (type value) on data by #'cddr 1533 (cl-loop for (type value) on data by #'cddr
1526 do 1534 do
1527 (cl-case type 1535 (cl-case type
@@ -1572,7 +1580,8 @@ function's documentation in the Info manual"))
1572 (:eg-result-string 1580 (:eg-result-string
1573 (insert " e.g. " double-arrow " ") 1581 (insert " e.g. " double-arrow " ")
1574 (princ value (current-buffer)) 1582 (princ value (current-buffer))
1575 (insert "\n"))))) 1583 (insert "\n"))))
1584 (add-text-properties start-example (point) `(shortdoc-example ,function)))
1576 ;; Insert the arglist after doing the evals, in case that's pulled 1585 ;; Insert the arglist after doing the evals, in case that's pulled
1577 ;; in the function definition. 1586 ;; in the function definition.
1578 (save-excursion 1587 (save-excursion
@@ -1582,6 +1591,48 @@ function's documentation in the Info manual"))
1582 (insert " " (symbol-name param))) 1591 (insert " " (symbol-name param)))
1583 (add-face-text-property arglist-start (point) 'shortdoc-section t)))) 1592 (add-face-text-property arglist-start (point) 'shortdoc-section t))))
1584 1593
1594(defun shortdoc-function-examples (function)
1595 "Return all shortdoc examples for FUNCTION.
1596The result is an alist with items of the form (GROUP . EXAMPLES),
1597where GROUP is a shortdoc group where FUNCTION appears, and
1598EXAMPLES is a string with the usage examples of FUNCTION defined
1599in GROUP. Return nil if FUNCTION is not a function or if it
1600doesn't has any shortdoc information."
1601 (let ((groups (and (symbolp function)
1602 (shortdoc-function-groups function)))
1603 (examples nil))
1604 (mapc
1605 (lambda (group)
1606 (with-temp-buffer
1607 (shortdoc--insert-group-in-buffer group)
1608 (goto-char (point-min))
1609 (let ((match (text-property-search-forward
1610 'shortdoc-example function t)))
1611 (push `(,group . ,(string-trim
1612 (buffer-substring-no-properties
1613 (prop-match-beginning match)
1614 (prop-match-end match))))
1615 examples))))
1616 groups)
1617 examples))
1618
1619(defun shortdoc-help-fns-examples-function (function)
1620 "Insert Emacs Lisp examples for FUNCTION into the current buffer.
1621You can add this function to the `help-fns-describe-function-functions'
1622hook to show examples of using FUNCTION in *Help* buffers produced
1623by \\[describe-function]."
1624 (let ((examples (shortdoc-function-examples function))
1625 (times 0))
1626 (dolist (example examples)
1627 (when (zerop times)
1628 (if (eq (length examples) 1)
1629 (insert "\n Example:\n\n")
1630 (insert "\n Examples:\n\n")))
1631 (setq times (1+ times))
1632 (insert " ")
1633 (insert (cdr example))
1634 (insert "\n\n"))))
1635
1585(defun shortdoc-function-groups (function) 1636(defun shortdoc-function-groups (function)
1586 "Return all shortdoc groups FUNCTION appears in." 1637 "Return all shortdoc groups FUNCTION appears in."
1587 (cl-loop for group in shortdoc--groups 1638 (cl-loop for group in shortdoc--groups
diff --git a/test/lisp/emacs-lisp/shortdoc-tests.el b/test/lisp/emacs-lisp/shortdoc-tests.el
index 516d095767f..a65a4a5ddc3 100644
--- a/test/lisp/emacs-lisp/shortdoc-tests.el
+++ b/test/lisp/emacs-lisp/shortdoc-tests.el
@@ -65,6 +65,16 @@
65 (when buf 65 (when buf
66 (kill-buffer buf)))))) 66 (kill-buffer buf))))))
67 67
68(ert-deftest shortdoc-function-examples-test ()
69 "Test the extraction of usage examples of some Elisp functions."
70 (should (equal '((list . "(delete 2 (list 1 2 3 4))\n => (1 3 4)\n (delete \"a\" (list \"a\" \"b\" \"c\" \"d\"))\n => (\"b\" \"c\" \"d\")"))
71 (shortdoc-function-examples 'delete)))
72 (should (equal '((alist . "(assq 'foo '((foo . bar) (zot . baz)))\n => (foo . bar)")
73 (list . "(assq 'b '((a . 1) (b . 2)))\n => (b . 2)"))
74 (shortdoc-function-examples 'assq)))
75 (should (equal '((regexp . "(string-match-p \"^[fo]+\" \"foobar\")\n => 0"))
76 (shortdoc-function-examples 'string-match-p))))
77
68(provide 'shortdoc-tests) 78(provide 'shortdoc-tests)
69 79
70;;; shortdoc-tests.el ends here 80;;; shortdoc-tests.el ends here
diff --git a/test/lisp/eshell/em-cmpl-tests.el b/test/lisp/eshell/em-cmpl-tests.el
index b60faab9114..ea907f1945d 100644
--- a/test/lisp/eshell/em-cmpl-tests.el
+++ b/test/lisp/eshell/em-cmpl-tests.el
@@ -261,7 +261,7 @@ See <lisp/eshell/esh-var.el>."
261 "Test completion of things that look like variable assignment, but aren't. 261 "Test completion of things that look like variable assignment, but aren't.
262For example, the second argument in \"tar --directory=dir\" looks 262For example, the second argument in \"tar --directory=dir\" looks
263like it could be a variable assignment, but it's not. We should 263like it could be a variable assignment, but it's not. We should
264let `pcomplete-tar' handle it instead. 264let `pcomplete/tar' handle it instead.
265 265
266See <lisp/eshell/esh-var.el>." 266See <lisp/eshell/esh-var.el>."
267 (with-temp-eshell 267 (with-temp-eshell