Ihor Radchenko <yanta...@posteo.net> writes:

> It has been a while since the last message in this thread.
> May I know if you need any further help with the patch?

My apologies for the radio silence, I was busy with school.

Please see the attached patch!

Will this do?

Rudy
>From ec7316614463d1c8217dac434fb8bad48446cd09 Mon Sep 17 00:00:00 2001
From: Rudolf Adamkovic <rud...@adamkovic.org>
Date: Sat, 24 Aug 2024 15:26:18 +0200
Subject: [PATCH] ox-texinfo: Support numeric values of `toc' in `#+OPTIONS'

* etc/ORG-NEWS (Texinfo exporter now supports numeric =toc= values in
=#+OPTIONS:=): Announce the change.
* lisp/ox.el (org-export-excluded-from-toc-p): Return nil for all
headlines that are above the `toc' value in the export `#+OPTIONS'.
(org-export-collect-headlines): Exclude all headlines that are above
the `toc' value in the export `#+OPTIONS' , using the updated
`org-export-excluded-from-toc-p' function.
* testing/lisp/test-ox-texinfo.el
(test-ox-texinfo/menus-nodes-headings): Add a test.
---
 etc/ORG-NEWS                    |  6 +++
 lisp/ox.el                      | 16 ++++---
 testing/lisp/test-ox-texinfo.el | 81 +++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+), 7 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 08857962b..3b2ca5f46 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -355,6 +355,12 @@ The Texinfo exporter no longer removes links from headings.  This
 applies to all headings, below and above the =H= and =toc= export
 =#+OPTIONS:=.
 
+*** Texinfo exporter now considers numeric =toc= values in =#+OPTIONS:=
+
+For example, given =H:3= and =toc:2= in =#+OPTIONS:=, all headings at
+the 1st and 2nd level appear in the table of contents and those at the
+3rd level do not.
+
 * Version 9.7
 
 ** Important announcements and breaking changes
diff --git a/lisp/ox.el b/lisp/ox.el
index 8410acc2b..6ddbd245c 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -5558,12 +5558,10 @@ Footnote sections are ignored."
 		     (+ (org-export-get-relative-level scope info) n))
 		   limit))))
     (org-element-map (org-element-contents scope) 'headline
-      (lambda (h)
-	(and (not (org-element-property :footnote-section-p h))
-	     (not (equal "notoc"
-		       (org-export-get-node-property :UNNUMBERED h t)))
-	     (>= n (org-export-get-relative-level h info))
-	     h))
+      (lambda (headline)
+        (and (not (org-export-excluded-from-toc-p headline info))
+             (>= n (org-export-get-relative-level headline info))
+             headline))
       info)))
 
 (defun org-export-collect-elements (type info &optional predicate)
@@ -5628,7 +5626,11 @@ contents.  However, it is useful if some additional processing is
 required on headlines excluded from table of contents."
   (or (org-element-property :footnote-section-p headline)
       (org-export-low-level-p headline info)
-      (equal "notoc" (org-export-get-node-property :UNNUMBERED headline t))))
+      (equal "notoc" (org-export-get-node-property :UNNUMBERED headline t))
+      (let ((toc-depth (plist-get info :with-toc)))
+        (and (wholenump toc-depth)
+             (> (org-export-get-relative-level headline info)
+                toc-depth)))))
 
 (defun org-export-toc-entry-backend (parent &rest transcoders)
   "Return an export backend appropriate for table of contents entries.
diff --git a/testing/lisp/test-ox-texinfo.el b/testing/lisp/test-ox-texinfo.el
index 681af7363..61f939ce1 100644
--- a/testing/lisp/test-ox-texinfo.el
+++ b/testing/lisp/test-ox-texinfo.el
@@ -484,5 +484,86 @@ body
          (should (search-forward "@uref{https://example.com, Foo@comma{} Bar}"))
          (should (search-forward "@uref{https://example.com, Foo@comma{} Bar@}}")))))))
 
+
+;;; Structure
+
+(ert-deftest test-ox-texinfo/menus-nodes-headings ()
+  "Test menus, nodes, and headings."
+  (should
+   (org-test-with-temp-text
+       (string-join
+        (list "#+OPTIONS: H:3 toc:2 num:nil"
+              "* Heading 1"
+              "** Heading 1-1"
+              "*** Heading 1-1-1"
+              "*** Heading 1-1-2"
+              "** Heading 1-2"
+              "*** Heading 1-2-1"
+              "*** Heading 1-2-2"
+              "* Heading 2"
+              "** Heading 2-1"
+              "*** Heading 2-1-1"
+              "*** Heading 2-1-2"
+              "** Heading 2-2"
+              "*** Heading 2-2-1"
+              "*** Heading 2-2-2")
+        "\n")
+     (let ((export-buffer "*Test Texinfo Export*")
+           (org-export-show-temporary-export-buffer nil))
+       (org-export-to-buffer 'texinfo export-buffer
+         nil nil nil nil nil
+         #'texinfo-mode)
+       (with-current-buffer export-buffer
+         (goto-char (point-min))
+         (and
+          (re-search-forward "^@menu$")
+          (re-search-forward "^* Heading 1::$")
+          (re-search-forward "^* Heading 2::$")
+          (re-search-forward "^@detailmenu$")
+          (re-search-forward "^Heading 1$")
+          (re-search-forward "^* Heading 1-1::$")
+          (re-search-forward "^* Heading 1-2::$")
+          (re-search-forward "^Heading 2$")
+          (re-search-forward "^* Heading 2-1::$")
+          (re-search-forward "^* Heading 2-2::$")
+          (re-search-forward "^@end detailmenu$")
+          (re-search-forward "^@end menu$")
+          (re-search-forward "^@node Heading 1$")
+          (re-search-forward "^@unnumbered Heading 1$")
+          (re-search-forward "^@menu$")
+          (re-search-forward "^* Heading 1-1::$")
+          (re-search-forward "^* Heading 1-2::$")
+          (re-search-forward "^@end menu$")
+          (re-search-forward "^@node Heading 1-1$")
+          (re-search-forward "^@unnumberedsec Heading 1-1$")
+          (re-search-forward "^@anchor{Heading 1-1-1}$")
+          (re-search-forward "^@subheading Heading 1-1-1$")
+          (re-search-forward "^@anchor{Heading 1-1-2}$")
+          (re-search-forward "^@subheading Heading 1-1-2$")
+          (re-search-forward "^@node Heading 1-2$")
+          (re-search-forward "^@unnumberedsec Heading 1-2$")
+          (re-search-forward "^@anchor{Heading 1-2-1}$")
+          (re-search-forward "^@subheading Heading 1-2-1$")
+          (re-search-forward "^@anchor{Heading 1-2-2}$")
+          (re-search-forward "^@subheading Heading 1-2-2$")
+          (re-search-forward "^@node Heading 2$")
+          (re-search-forward "^@unnumbered Heading 2$")
+          (re-search-forward "^@menu$")
+          (re-search-forward "^* Heading 2-1::$")
+          (re-search-forward "^* Heading 2-2::$")
+          (re-search-forward "^@end menu$")
+          (re-search-forward "^@node Heading 2-1$")
+          (re-search-forward "^@unnumberedsec Heading 2-1$")
+          (re-search-forward "^@anchor{Heading 2-1-1}$")
+          (re-search-forward "^@subheading Heading 2-1-1$")
+          (re-search-forward "^@anchor{Heading 2-1-2}$")
+          (re-search-forward "^@subheading Heading 2-1-2$")
+          (re-search-forward "^@node Heading 2-2$")
+          (re-search-forward "^@unnumberedsec Heading 2-2$")
+          (re-search-forward "^@anchor{Heading 2-2-1}$")
+          (re-search-forward "^@subheading Heading 2-2-1$")
+          (re-search-forward "^@anchor{Heading 2-2-2}$")
+          (re-search-forward "^@subheading Heading 2-2-2$")))))))
+
 (provide 'test-ox-texinfo)
 ;;; test-ox-texinfo.el end here
-- 
2.39.5 (Apple Git-154)

-- 
"The whole science is nothing more than a refinement of everyday
thinking."  --- Albert Einstein, 1879-1955

Rudolf Adamkovič <rud...@adamkovic.org> [he/him]
http://adamkovic.org

Reply via email to