diff options
| author | Juanma Barranquero | 2019-11-23 23:29:53 +0100 |
|---|---|---|
| committer | Juanma Barranquero | 2019-11-23 23:29:53 +0100 |
| commit | 6f3ff47c521a41f3eab3efd1f6126f06f4171478 (patch) | |
| tree | 3dc30a4fa7a2d75cfaf0702c3a6dc6520ead0726 | |
| parent | 4b5d04be44af36cb2faccd368de063cf376282ca (diff) | |
| download | emacs-6f3ff47c521a41f3eab3efd1f6126f06f4171478.tar.gz emacs-6f3ff47c521a41f3eab3efd1f6126f06f4171478.zip | |
Make help-split-fundoc more flexible about what returns
* lisp/help.el (help-split-fundoc): New arg SECTION to return
only the usage or doc parts of the docstring, or both even if
there is no usage.
* test/lisp/help-tests.el: New file.
| -rw-r--r-- | lisp/help.el | 44 | ||||
| -rw-r--r-- | test/lisp/help-tests.el | 56 |
2 files changed, 84 insertions, 16 deletions
diff --git a/lisp/help.el b/lisp/help.el index 22f35df1de1..06264ae2f32 100644 --- a/lisp/help.el +++ b/lisp/help.el | |||
| @@ -1380,27 +1380,39 @@ The result, when formatted by `substitute-command-keys', should equal STRING." | |||
| 1380 | ;; But for various reasons, they are more widely needed, so they were | 1380 | ;; But for various reasons, they are more widely needed, so they were |
| 1381 | ;; moved to this file, which is preloaded. https://debbugs.gnu.org/17001 | 1381 | ;; moved to this file, which is preloaded. https://debbugs.gnu.org/17001 |
| 1382 | 1382 | ||
| 1383 | (defun help-split-fundoc (docstring def) | 1383 | (defun help-split-fundoc (docstring def &optional section) |
| 1384 | "Split a function DOCSTRING into the actual doc and the usage info. | 1384 | "Split a function DOCSTRING into the actual doc and the usage info. |
| 1385 | Return (USAGE . DOC) or nil if there's no usage info, where USAGE info | 1385 | Return (USAGE . DOC), where USAGE is a string describing the argument |
| 1386 | is a string describing the argument list of DEF, such as | 1386 | list of DEF, such as \"(apply FUNCTION &rest ARGUMENTS)\". |
| 1387 | \"(apply FUNCTION &rest ARGUMENTS)\". | 1387 | DEF is the function whose usage we're looking for in DOCSTRING. |
| 1388 | DEF is the function whose usage we're looking for in DOCSTRING." | 1388 | With SECTION nil, return nil if there is no usage info; conversely, |
| 1389 | SECTION t means to return (USAGE . DOC) even if there's no usage info. | ||
| 1390 | When SECTION is \\='usage or \\='doc, return only that part." | ||
| 1389 | ;; Functions can get the calling sequence at the end of the doc string. | 1391 | ;; Functions can get the calling sequence at the end of the doc string. |
| 1390 | ;; In cases where `function' has been fset to a subr we can't search for | 1392 | ;; In cases where `function' has been fset to a subr we can't search for |
| 1391 | ;; function's name in the doc string so we use `fn' as the anonymous | 1393 | ;; function's name in the doc string so we use `fn' as the anonymous |
| 1392 | ;; function name instead. | 1394 | ;; function name instead. |
| 1393 | (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)) | 1395 | (let* ((found (and docstring |
| 1394 | (let ((doc (unless (zerop (match-beginning 0)) | 1396 | (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring))) |
| 1395 | (substring docstring 0 (match-beginning 0)))) | 1397 | (doc (if found |
| 1396 | (usage-tail (match-string 1 docstring))) | 1398 | (and (memq section '(t nil doc)) |
| 1397 | (cons (format "(%s%s" | 1399 | (not (zerop (match-beginning 0))) |
| 1398 | ;; Replace `fn' with the actual function name. | 1400 | (substring docstring 0 (match-beginning 0))) |
| 1399 | (if (symbolp def) | 1401 | docstring)) |
| 1400 | (help--docstring-quote (format "%S" def)) | 1402 | (usage (and found |
| 1401 | 'anonymous) | 1403 | (memq section '(t nil usage)) |
| 1402 | usage-tail) | 1404 | (let ((tail (match-string 1 docstring))) |
| 1403 | doc)))) | 1405 | (format "(%s%s" |
| 1406 | ;; Replace `fn' with the actual function name. | ||
| 1407 | (if (and (symbolp def) def) | ||
| 1408 | (help--docstring-quote (format "%S" def)) | ||
| 1409 | 'anonymous) | ||
| 1410 | tail))))) | ||
| 1411 | (pcase section | ||
| 1412 | (`nil (and usage (cons usage doc))) | ||
| 1413 | (`t (cons usage doc)) | ||
| 1414 | (`usage usage) | ||
| 1415 | (`doc doc)))) | ||
| 1404 | 1416 | ||
| 1405 | (defun help-add-fundoc-usage (docstring arglist) | 1417 | (defun help-add-fundoc-usage (docstring arglist) |
| 1406 | "Add the usage info to DOCSTRING. | 1418 | "Add the usage info to DOCSTRING. |
diff --git a/test/lisp/help-tests.el b/test/lisp/help-tests.el new file mode 100644 index 00000000000..28dd8830e44 --- /dev/null +++ b/test/lisp/help-tests.el | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | ;;; help-tests.el --- Tests for help.el -*- lexical-binding: t; -*- | ||
| 2 | |||
| 3 | ;; Copyright (C) 2019 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Juanma Barranquero <lekktu@gmail.com> | ||
| 6 | ;; Keywords: help, internal | ||
| 7 | |||
| 8 | ;; This file is part of GNU Emacs. | ||
| 9 | |||
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | ||
| 11 | ;; it under the terms of the GNU General Public License as published by | ||
| 12 | ;; the Free Software Foundation, either version 3 of the License, or | ||
| 13 | ;; (at your option) any later version. | ||
| 14 | |||
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | ;; GNU General Public License for more details. | ||
| 19 | |||
| 20 | ;; You should have received a copy of the GNU General Public License | ||
| 21 | ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. | ||
| 22 | |||
| 23 | ;;; Code: | ||
| 24 | |||
| 25 | (require 'ert) | ||
| 26 | |||
| 27 | (ert-deftest help-split-fundoc-SECTION () | ||
| 28 | "Test new optional arg SECTION." | ||
| 29 | (let* ((doc "Doc first line.\nDoc second line.") | ||
| 30 | (usg "\n\n(fn ARG1 &optional ARG2)") | ||
| 31 | (full (concat doc usg)) | ||
| 32 | (usage "(t ARG1 &optional ARG2)")) | ||
| 33 | ;; Docstring has both usage and doc | ||
| 34 | (should (equal (help-split-fundoc full t nil) `(,usage . ,doc))) | ||
| 35 | (should (equal (help-split-fundoc full t t) `(,usage . ,doc))) | ||
| 36 | (should (equal (help-split-fundoc full t 'usage) usage)) | ||
| 37 | (should (equal (help-split-fundoc full t 'doc) doc)) | ||
| 38 | ;; Docstring has no usage, only doc | ||
| 39 | (should (equal (help-split-fundoc doc t nil) nil)) | ||
| 40 | (should (equal (help-split-fundoc doc t t) `(nil . ,doc))) | ||
| 41 | (should (equal (help-split-fundoc doc t 'usage) nil)) | ||
| 42 | (should (equal (help-split-fundoc doc t 'doc) doc)) | ||
| 43 | ;; Docstring is only usage, no doc | ||
| 44 | (should (equal (help-split-fundoc usg t nil) `(,usage . nil))) | ||
| 45 | (should (equal (help-split-fundoc usg t t) `(,usage . nil))) | ||
| 46 | (should (equal (help-split-fundoc usg t 'usage) usage)) | ||
| 47 | (should (equal (help-split-fundoc usg t 'doc) nil)) | ||
| 48 | ;; Docstring is null | ||
| 49 | (should (equal (help-split-fundoc nil t nil) nil)) | ||
| 50 | (should (equal (help-split-fundoc nil t t) '(nil))) | ||
| 51 | (should (equal (help-split-fundoc nil t 'usage) nil)) | ||
| 52 | (should (equal (help-split-fundoc nil t 'doc) nil)))) | ||
| 53 | |||
| 54 | (provide 'help-tests) | ||
| 55 | |||
| 56 | ;;; help-tests.el ends here | ||