Welcome to little lamb

Code » qmdoc » commit b0f5db9

TOC: Add special sorting for man pages..

author Olivier Brunel
2023-07-07 13:41:57 UTC
committer Olivier Brunel
2023-07-07 16:59:36 UTC
parent 8ae8c8ea9d8389978924e82693674f66e722765a

TOC: Add special sorting for man pages..

That is, when sorting two pages that both have a title that ends with a
number between 0 and 9 (both inc.) in between parenthesis (e.g. foo(3))
and both section numbers are different, sort by section first.

Point is, when having a mix of pages in different sections, first will
be all pages in section 0, then 1, etc.

src/doc/qmdoc.1.md +9 -0
src/qmdoc/qmdoc.c +13 -0

diff --git a/src/doc/qmdoc.1.md b/src/doc/qmdoc.1.md
index 6500fa9..86660d0 100644
--- a/src/doc/qmdoc.1.md
+++ b/src/doc/qmdoc.1.md
@@ -359,6 +359,15 @@ This can be changed using the `--sort-group` option. Possible values are `title`
 (default) and `file`, to use file names. One can also prefix the value with `d:`
 to have them sorted in descending order.
 
+! WARN: Special man-page sorting
+! Note that when sorting by page's title, a special handling is done for pages
+! whose titles are matching a man page, i.e. their title ends with a number
+! between 0 and 9 (both included) in parenthesis.
+!
+! When two such pages are compared, and are not in the same section (the number
+! in between the parenthesis), then pages are ordered based on their section
+! number.
+
 ## Sorting groups
 
 Two kinds of groups are actually supported : regular groups, and sorting groups.
diff --git a/src/qmdoc/qmdoc.c b/src/qmdoc/qmdoc.c
index 9904d91..748f145 100644
--- a/src/qmdoc/qmdoc.c
+++ b/src/qmdoc/qmdoc.c
@@ -2127,6 +2127,7 @@ cmp_page(const void *i1, const void *i2, void *cmp_)
     struct qmdoc *ctx = cmp->ctx;
     const int * const i[2] = { i1, i2 };
     size_t off[2];
+    int man[2] = { -1, -1 };
 
     for (int j = 0; j < 2; ++j) {
         struct entry *e = &ENTRY(ctx)[*i[j]];
@@ -2137,8 +2138,20 @@ cmp_page(const void *i1, const void *i2, void *cmp_)
         } else {
             off[j] = e->noff;
         }
+
+        if (cmp->sort == SORT_TITLE) {
+            const char *s;
+            size_t l;
+            s = cmp->sa->s + off[j];
+            l = strlen(s);
+            man[j] = (l >= 4 && s[l - 1] == ')' && s[l - 3] == '('
+                      && s[l - 2] >= '0' && s[l - 2] <= '9') ? s[l - 2] : -1;
+        }
     }
 
+    if (man[0] >= '0' && man[1] >= '0' && man[0] != man[1])
+        return (man[0] - man[1]) * ((cmp->desc) ? -1 : 1);
+
     int r = strcoll(cmp->sa->s + off[0], cmp->sa->s + off[1]);
     return (!r) ? r : (cmp->desc) ? -r : r;
 }