From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id GMOqLqE+UGMNWQEAbAwnHQ (envelope-from ) for ; Wed, 19 Oct 2022 20:14:57 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id lVLNLqE+UGPwqgAA9RJhRA (envelope-from ) for ; Wed, 19 Oct 2022 20:14:57 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 1702C29580 for ; Wed, 19 Oct 2022 20:14:57 +0200 (CEST) Received: from localhost ([::1]:43628 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olDah-0005vP-M7 for larch@yhetil.org; Wed, 19 Oct 2022 14:14:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40296) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olDZU-0005vE-Ky for emacs-orgmode@gnu.org; Wed, 19 Oct 2022 14:13:40 -0400 Received: from smtp6-g21.free.fr ([212.27.42.6]:33924) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olDZS-0005gr-7Q for emacs-orgmode@gnu.org; Wed, 19 Oct 2022 14:13:40 -0400 Received: from tosh-laptop (unknown [IPv6:2a01:e0a:505:3460:1a16:a0c4:3f89:c0d9]) by smtp6-g21.free.fr (Postfix) with ESMTPS id 8E82478034D; Wed, 19 Oct 2022 20:13:32 +0200 (CEST) Received: by tosh-laptop (sSMTP sendmail emulation); Wed, 19 Oct 2022 20:18:37 +0200 From: Bruno Barbier To: Ihor Radchenko Cc: Damien Cassou , emacs-orgmode@gnu.org Subject: Re: PATCH: Re: Reading the parameters of a special-block In-Reply-To: <871qr5hgr1.fsf@localhost> References: <87zgdu26dd.fsf@cassou.me> <871qr5hgr1.fsf@localhost> Date: Wed, 19 Oct 2022 20:18:37 +0200 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: softfail client-ip=212.27.42.6; envelope-from=brubar.cs@gmail.com; helo=smtp6-g21.free.fr X-Spam_score_int: 12 X-Spam_score: 1.2 X-Spam_bar: + X-Spam_report: (1.2 / 5.0 requ) BAYES_00=-1.9, DKIM_ADSP_CUSTOM_MED=0.001, FORGED_GMAIL_RCVD=1, FREEMAIL_FROM=0.001, MISSING_MID=0.497, NML_ADSP_CUSTOM_MED=0.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_SOFTFAIL=0.665 autolearn=no autolearn_force=no X-Spam_action: no action Message-Id: X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1666203297; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post; bh=ndW9epZAYql5xOiJuJZsaEjuK5/JnfitJ7+ATEJLR74=; b=Xl6bEwT2cETr2Ok7z1rkYBjyXZjiThns4uEthXdazSiVLuDzU0m3cWNpooNIIvSfvS7MKk 2YNJhKWPB9N4ZHsT8jSbZ2jaAi09ikn5j+ZBCUduZnfjo6ArwFcUXutS7XhfYBgDOZ1SBG Fw66TA9DciVL/aoPhfUBV5x0xTK7KEDuPue8iP2zHq+3YF1mAhxz/ZfqnZfGqNj6Zhl97m 5ijfaxoZiZf3aciQgAxkIU7XT2h/iRVyWqzoS2cKrFYQwGnmfQG8wNDjywyCjXbbeonezV zMCdvsigX8kTzRjIcHmdhzSToBPPYWneOVdqJESyU1OKOs5WC57Tt7sJrVgfSA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1666203297; a=rsa-sha256; cv=none; b=PuvThbJq34783mfu/05grS31nY+3v8hWNv15rl8hZM41hBbeCLKWbOBC2/DaIsnjazxVYH 3EJAacb6KVMQ0n+9oAbRy4vhHHOyOVL7gYKJl1BKtiJOdA7FSGmUtWa////U9N1mZLPTah cwB9sUnUkXO6WoWlpvIU8n732Hsu9j9yu+abpY6JZx6sHYK1xIjxr5fgKPEBjUDeGJuwy1 +cbhsyVMc0ThIKaifypq5FK52LRuy2c9C5212Ca+CDXNU/qr8DOYo5dEubA+edeQdqLHZK ZgiJUxW7WiSHu1L0NbQy922mfna7T1FS6y25/kUrpQBUvwPdhOdEXcnvz9OfOg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 3.77 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 1702C29580 X-Spam-Score: 3.77 X-Migadu-Scanner: scn1.migadu.com X-TUID: uLR12ysSkayN --=-=-= Content-Type: text/plain Ihor Radchenko writes: > The property name could be simply :parameters. Just like in src blocks > (see org-element-src-block-parser). Indeed. Done. > We probably want something like > > :parameters (and (org-string-nw-p parameters) (org-trim parameters)) > > Just as in org-element-src-block-parser. Actually, I forgot to raise the question; but, you answered it anyway! ;-) Done. > > No dangling ")" please. Sorry. Fixed. > >> + (format "#+begin_%s%s%s\n%s#+end_%s" block-type >> + (if (string= "" block-parameters-line) "" " ") block-parameters-line >> + contents block-type))) > > We will not need to test against ="" with my above comment incorporated. For `:parameters', I'm now doing the same as in `org-element-src-block-interpreter'. > May also test against empty parameters and space-only parameters. I added more tests. I also added a new independent patch, to fix a bug when interpreting a special block that has no content, and some tests. Let me know if I did miss something, And thank you for the review! Bruno --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-org-element-special-block-interpreter-Fix-when-no-co.patch Content-Description: Fix no content >From ca4ab60a11330b97e1fb8c8d2941e383189fd82e Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Wed, 19 Oct 2022 00:37:05 +0200 Subject: [PATCH 1/2] org-element-special-block-interpreter: Fix when no content * lisp/org-element.el (org-element-special-block-interpreter): Use empty string when content is nil. * testing/lisp/test-org-element.el (test-org-element/special-block-interpreter): Test the case with no content. --- lisp/org-element.el | 2 +- testing/lisp/test-org-element.el | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 7b26e877e..ca3b61b49 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1910,7 +1910,7 @@ (defun org-element-special-block-interpreter (special-block contents) "Interpret SPECIAL-BLOCK element as Org syntax. CONTENTS is the contents of the element." (let ((block-type (org-element-property :type special-block))) - (format "#+begin_%s\n%s#+end_%s" block-type contents block-type))) + (format "#+begin_%s\n%s#+end_%s" block-type (or contents "") block-type))) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 187cadf7a..985108e37 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -2425,7 +2425,11 @@ (ert-deftest test-org-element/special-block-parser () ;; Handle non-empty blank line at the end of buffer. (should (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n " - (= (org-element-property :end (org-element-at-point)) (point-max))))) + (= (org-element-property :end (org-element-at-point)) (point-max)))) + ;; When contents is empty, the parsed contents is nil. + (should + (org-test-with-temp-text "#+BEGIN_SPECIAL\n#+END_SPECIAL" + (eq nil (org-element-contents (org-element-at-point)))))) ;;;; Src Block @@ -2943,7 +2947,11 @@ (ert-deftest test-org-element/special-block-interpreter () "Test special block interpreter." (should (equal (org-test-parse-and-interpret "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL") - "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n"))) + "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")) + ;; No content + (should (equal (org-test-parse-and-interpret + "#+BEGIN_SPECIAL\n#+END_SPECIAL") + "#+begin_SPECIAL\n#+end_SPECIAL\n"))) (ert-deftest test-org-element/babel-call-interpreter () "Test Babel call interpreter." -- 2.37.3 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-lisp-org-element-Add-a-parameters-line-property-to-s.patch Content-Description: Add :parameters to special blocks >From 584bdbc161abde761876bfa2f0c484f0363afb72 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Mon, 17 Oct 2022 20:19:02 +0200 Subject: [PATCH 2/2] lisp/org-element: Add a parameters-line property to special blocks Add a property `:parameters' to special blocks, to store the PARAMETERS as a string. * lisp/org-element.el (org-element-special-block-parser): Parse PARAMETERS and set the property `:parameters'. (org-element-special-block-interpreter): Interpret the property `:parameters'. * testing/lisp/test-org-element.el (test-org-element/special-block-parser): Add a new test for PARAMETERS. (test-org-element/special-block-interpreter): Add new tests for PARAMETERS. --- lisp/org-element.el | 19 ++++++++++++------ testing/lisp/test-org-element.el | 33 ++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index ca3b61b49..ce6daaef8 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1868,13 +1868,15 @@ (defun org-element-special-block-parser (limit affiliated) their value. Return a list whose CAR is `special-block' and CDR is a plist -containing `:type', `:begin', `:end', `:contents-begin', -`:contents-end', `:post-blank' and `:post-affiliated' keywords. +containing `:type', `:parameters', `:begin', `:end', +`:contents-begin', `:contents-end', `:post-blank' and +`:post-affiliated' keywords. Assume point is at the beginning of the block." (let* ((case-fold-search t) - (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)") - (match-string-no-properties 1)))) + (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)[ \t]*\\(.*\\)[ \t]*$") + (match-string-no-properties 1))) + (parameters (match-string-no-properties 2))) (if (not (save-excursion (re-search-forward (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type)) @@ -1898,6 +1900,8 @@ (defun org-element-special-block-parser (limit affiliated) (list 'special-block (nconc (list :type type + :parameters (and (org-string-nw-p parameters) + (org-trim parameters)) :begin begin :end end :contents-begin contents-begin @@ -1909,8 +1913,11 @@ (defun org-element-special-block-parser (limit affiliated) (defun org-element-special-block-interpreter (special-block contents) "Interpret SPECIAL-BLOCK element as Org syntax. CONTENTS is the contents of the element." - (let ((block-type (org-element-property :type special-block))) - (format "#+begin_%s\n%s#+end_%s" block-type (or contents "") block-type))) + (let ((block-type (org-element-property :type special-block)) + (parameters (org-element-property :parameters special-block))) + (format "#+begin_%s%s\n%s#+end_%s" block-type + (if parameters (concat " " parameters) "") + (or contents "") block-type))) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 985108e37..eb5b95e86 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -2429,7 +2429,19 @@ (ert-deftest test-org-element/special-block-parser () ;; When contents is empty, the parsed contents is nil. (should (org-test-with-temp-text "#+BEGIN_SPECIAL\n#+END_SPECIAL" - (eq nil (org-element-contents (org-element-at-point)))))) + (eq nil (org-element-contents (org-element-at-point))))) + ;; Parse parameters if any, trimming blanks. + (should + (org-test-with-temp-text "#+BEGIN_SPECIAL* s p :w 3 \nContent.\n#+END_SPECIAL*" + (equal "s p :w 3" + (org-element-property :parameters (org-element-at-point))))) + ;; When parameters is blank, `:parameters' is nil. + (should + (org-test-with-temp-text "#+BEGIN_SPECIAL* \t \nContent.\n#+END_SPECIAL*" + (eq nil (org-element-property :parameters (org-element-at-point)))) + )) + + ;;;; Src Block @@ -2945,13 +2957,30 @@ (ert-deftest test-org-element/quote-block-interpreter () (ert-deftest test-org-element/special-block-interpreter () "Test special block interpreter." + ;; No parameters (should (equal (org-test-parse-and-interpret "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL") "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")) ;; No content (should (equal (org-test-parse-and-interpret "#+BEGIN_SPECIAL\n#+END_SPECIAL") - "#+begin_SPECIAL\n#+end_SPECIAL\n"))) + "#+begin_SPECIAL\n#+end_SPECIAL\n")) + ;; Some parameters + (should + (equal (org-test-parse-and-interpret + "#+BEGIN_special some parameters until EOL\nA very special content\n#+END_special") + "#+begin_special some parameters until EOL\nA very special content\n#+end_special\n")) + ;; No parameters (blanks only) + (should + (equal (org-test-parse-and-interpret + "#+BEGIN_special \t \nA very special content\n#+END_special") + "#+begin_special\nA very special content\n#+end_special\n")) + ;; Some parameters with leading and trailing blanks, no content, and + ;; a /special/ name. + (should + (equal (org-test-parse-and-interpret + "#+BEGIN_speial :a :b \t :c \t \n#+END_speial") + "#+begin_speial :a :b \t :c\n#+end_speial\n"))) (ert-deftest test-org-element/babel-call-interpreter () "Test Babel call interpreter." -- 2.37.3 --=-=-=--