From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jens Lechtenboerger Subject: [PATCH] Derive non-default start value for ordered list Date: Sun, 01 Dec 2019 21:45:06 +0100 Message-ID: <87r21nvkq5.fsf@informationelle-selbstbestimmung-im-internet.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:470:142:3::10]:46214) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ibW5k-0000kp-17 for emacs-orgmode@gnu.org; Sun, 01 Dec 2019 15:45:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ibW5i-0000yR-Av for emacs-orgmode@gnu.org; Sun, 01 Dec 2019 15:45:15 -0500 Received: from mx2.mailbox.org ([80.241.60.215]:52275) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ibW5h-0000xg-V7 for emacs-orgmode@gnu.org; Sun, 01 Dec 2019 15:45:14 -0500 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-CHACHA20-POLY1305 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 4589DA250C for ; Sun, 1 Dec 2019 21:45:11 +0100 (CET) Received: from smtp1.mailbox.org ([80.241.60.240]) by spamfilter06.heinlein-hosting.de (spamfilter06.heinlein-hosting.de [80.241.56.125]) (amavisd-new, port 10030) with ESMTP id wB-Bt6gSvqiD for ; Sun, 1 Dec 2019 21:45:07 +0100 (CET) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi there, currently, we have to write the following to continue an ordered list from a value different from 1: 42. [@42] Answer 43. Question? The requirement to type redundant information with the @-syntax always struck me as odd. For my export backend org-re-reveal, I recently received a request to export lists without @-syntax to their =E2=80=9Ccorrect=E2=80=9D start values [1]. Before working on my backend, I=E2=80=99d like to ask for feedback: Why was the @-syntax introduced? Of what non-obvious effects should I be aware? What do you think about the attached patch that allows to omit the @-syntax? Controlled by the new variable org-list-use-first-bullet-as-non-standard-counter, the code assigns a counter value to the first list item from its bullet string if the item 1. does not specify a counter itself, 2. has an alphanumeric bullet, and 3. does not have a default start value (1, a, A). I hacked this as postprocessing step on the list=E2=80=99s struct. Maybe an Org expert could suggest how to do this in one pass? Best wishes Jens P.S. I did not work on documentation yet as I=E2=80=99m not sure that this change is acceptable. [1] https://gitlab.com/oer/org-re-reveal/merge_requests/27 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Use-bullet-as-non-standard-counter.patch >From 2eea43d84687259633f847bd17883e5fe578b6bc Mon Sep 17 00:00:00 2001 From: Jens Lechtenboerger Date: Sun, 1 Dec 2019 21:17:43 +0100 Subject: [PATCH] Use bullet as non-standard counter * lisp/org-list.el: New variable org-list-use-first-bullet-as-non-standard-counter and new function org-list-struct-maybe-add-counters to assign counters from bullets that indicate non-standard start values. (org-list-struct): Use new variable and function. * lisp/org-element.el (org-element-item-parser): Use new variable and mirror behavior of org-list.el. --- lisp/org-element.el | 19 ++++++++++++++++++- lisp/org-list.el | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 56b3cc413..80b7cab99 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1269,6 +1269,8 @@ Assume point is at the beginning of the item." (beginning-of-line) (looking-at org-list-full-item-re) (let* ((begin (point)) + (prevs (org-list-prevs-alist struct)) + (prev-item (org-list-get-prev-item begin struct prevs)) (bullet (match-string-no-properties 1)) (checkbox (let ((box (match-string 3))) (cond ((equal "[ ]" box) 'off) @@ -1277,7 +1279,22 @@ Assume point is at the beginning of the item." (counter (let ((c (match-string 2))) (save-match-data (cond - ((not c) nil) + ((not c) + (and + org-list-use-first-bullet-as-non-standard-counter + ;; As in org-list-struct-maybe-add-counters, + ;; assign non-standard counter from bullet of + ;; first list item. To exclude "A", check range + ;; from "B". + (not prev-item) + (or (and (string-match "[B-Zb-z]" bullet) + (- (string-to-char + (upcase (match-string 0 bullet))) + 64)) + (and (string-match "[0-9]+" bullet) + (string< "1" (match-string 0 bullet)) + (string-to-number + (match-string 0 bullet)))))) ((string-match "[A-Za-z]" c) (- (string-to-char (upcase (match-string 0 c))) 64)) diff --git a/lisp/org-list.el b/lisp/org-list.el index c4aef32fc..fe8742f42 100644 --- a/lisp/org-list.el +++ b/lisp/org-list.el @@ -336,6 +336,14 @@ clearly distinguish sub-items in a list." :version "24.1" :type 'integer) +(defcustom org-list-use-first-bullet-as-non-standard-counter nil + "Non-nil means to use first bullet of an ordered list as counter. +Then, you can start an ordered list with \"42. Answer\" instead of +\"42. [@42] Answer\"." + :group 'org-plain-lists + :package-version '(Org . "9.3") + :type 'boolean) + (defvar org-list-forbidden-blocks '("example" "verse" "src" "export") "Names of blocks where lists are not allowed. Names must be in lower case.") @@ -731,7 +739,30 @@ Assume point is at an item." ;; 3. Associate each item to its end position. (org-list-struct-assoc-end struct end-lst) ;; 4. Return STRUCT - struct))) + (if org-list-use-first-bullet-as-non-standard-counter + (org-list-struct-maybe-add-counters struct) + struct)))) + +(defun org-list-struct-maybe-add-counters (struct) + "Maybe add counters to first list items of STRUCT. +For the first list items in STRUCT (those without previous item) that +1. do not specify a counter, +2. has an alphanumeric bullet, and +3. do not have a default start value (1, a, A), +set the counter to the item's bullet." + (let ((prevs (org-list-prevs-alist struct))) + (dolist (item struct) + (let ((prev-item (org-list-get-prev-item (nth 0 item) struct prevs))) + (unless prev-item + (let ((key (nth 0 item)) + (fbullet (nth 2 item)) + (fcounter (nth 3 item))) + (when (and (not fcounter) + (string-match "[[:alnum:]]" fbullet)) + (let ((cand (match-string 0 fbullet))) + (unless (member cand '("1" "a" "A")) + (org-list-set-nth 3 key struct cand)))))))) + struct)) (defun org-list-struct-assoc-end (struct end-list) "Associate proper ending point to items in STRUCT. -- 2.17.1 --=-=-=--