From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1.migadu.com ([2001:41d0:1008:1e59::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms8.migadu.com with LMTPS id uCS1MZJ4lWWCFwAAkFu2QA (envelope-from ) for ; Wed, 03 Jan 2024 16:09:06 +0100 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1.migadu.com with LMTPS id eGY3K5J4lWXGRgEA62LTzQ (envelope-from ) for ; Wed, 03 Jan 2024 16:09:06 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=hyOvp6ro; 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"; dmarc=pass (policy=none) header.from=posteo.net ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1704294546; 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:dkim-signature; bh=yBaduZ1UbVQxzHzbZsZ/bBy/VAL9Rb1pr6JAoCWfcuY=; b=DCneHnZtO73zpHMzNl9zGO4K4gOYb9hYD6sYQ38NIn6TWUWDyaacLGsKct2oh5+P6enXP9 G0BE6zBsQ2Ci7VdYyQdH+8Qyh4TmteY8Z7bDUWuBLavfLJdmbEGImByTr73+VKsi0tecDy 9ia35D6lEfco6z0rYIn6XDEyNYA6a1XTl28qm1m9Q6/lMaMa1cxCOJUMw/wrCGYk+A27Kf ppW2xRJBqGe8abaHnAnHagzjHmOfXlmPEul7cHDh7ewOBkmsrb4FVf4357Gx7rZVXN0Gzr UVb5uSIlQZasayZiUmP+QTI6GdxvBp7aTul4xY6iuPVRS/XIni4OT6MYgiOsTA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=posteo.net header.s=2017 header.b=hyOvp6ro; 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"; dmarc=pass (policy=none) header.from=posteo.net ARC-Seal: i=1; s=key1; d=yhetil.org; t=1704294546; a=rsa-sha256; cv=none; b=MKctrIhmMaIH2yowxJFltzl+OcoAWvYE+KZE+g+z4h9VN2AUA5KlPykyglra8pt8IuUWtQ 3aBs2Db1DbbKlcnBivV0V9Rrp5Z8ArQhnH5y/E/x46Nxol7M7r7dIQHZxx8r8eTu61NNQb 6InH6sMdY1/vgqqYh49XwMlxYZZ17tLuXgmN4tgIgtMoMNfxJj76tYckUP2MZY5IyfulJt aV0CCzyaimeCYY0mzKEiG9PeIU8rEb1fqONFls3sElaRLISXx2FVVI49AVUxG13Jvg521/ E0GGDZZ/p6ailUTNkvuT761gO+PKjKvmjpUU77ZEC00wY4qav5Xu6YDsXIS5SQ== 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 48E8540F6B for ; Wed, 3 Jan 2024 16:09:04 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rL2qi-0004bW-5h; Wed, 03 Jan 2024 10:08:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rL2qg-0004bO-0N for emacs-orgmode@gnu.org; Wed, 03 Jan 2024 10:08:02 -0500 Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rL2qc-0003jP-Su for emacs-orgmode@gnu.org; Wed, 03 Jan 2024 10:08:01 -0500 Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 5E8E9240027 for ; Wed, 3 Jan 2024 16:07:55 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1704294475; bh=Uw02gQgypbP3QYVuGpSGBV1RM2rcsXXYv2jmEYplYL4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:From; b=hyOvp6ro59qqMpSTa2MhA8ggzChOrgVsF1zvK0SuQ8mOGzr6Iek9Dk85jVivbXcCq BB4ATNvXeY+7neRhQo0B1vEiWV6y+TK7VPLNeGQFpQH0hwb+u9Kg7GAT7vOe/mr/7D dUqsI48usFDog/h0Gfaen8z+2+SwoLSQwg8N8zJFjPluCktmOyFKZU3l1KGhPLdxdr c+zWlN/9gYZYBt6NSHxi1YlfMApeYGGX4jXiwtujCmpjknEj95q9XccgtzTElYePQi YjX3QYSSHsDLo0CPCsZ91IiCMiUFcVf30Xhbw53voqiCexrIhtcmpf/mxYmwRKtTI6 wXhnWHZpqNdJw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4T4tPk3ldcz6tvs; Wed, 3 Jan 2024 16:07:54 +0100 (CET) From: Ihor Radchenko To: gerard.vermeulen@posteo.net Cc: Emacs orgmode Subject: Re: [PATCH] org-babel-demarcate-block: split using org-element instead of regexp In-Reply-To: References: <7e41f9b6e9026a404e256f33371e974c@posteo.net> <87zfxq78ff.fsf@localhost> <87zfxo3taa.fsf@localhost> Date: Wed, 03 Jan 2024 15:11:06 +0000 Message-ID: <87v88a78qd.fsf@localhost> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=185.67.36.65; envelope-from=yantar92@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action 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-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN X-Migadu-Scanner: mx13.migadu.com X-Migadu-Spam-Score: -7.16 X-Spam-Score: -7.16 X-Migadu-Queue-Id: 48E8540F6B X-TUID: +qbVoisL4jOj --=-=-= Content-Type: text/plain gerard.vermeulen@posteo.net writes: > Attached you'll find a new patch trying to implement your suggestions. > Interactive splitting by demarcation seems to work quite well (see the > before and after splitting snippets in the PS). Thanks! > However, I cannot run the test because org-babel-demarcate-block > always errors "org-element--cache: Emergency exit" while the same > input works interactively. Could there be a problem of cache > synchronization or something like that? Is there something I can do? This was a bug in `org-element-copy'. Fixed, on main now. https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=dfeff03c5 > I also did not yet look into how to propagate a switch like -n10. -n10 is not a valid switch, AFAIK. We demand space: -n 10. See 12.6 Literal Examples section of the manual. I made some adjustments to the patch, making use of org-element API. See the attached updated version of the patch. I am not yet merging it as I found some weirdness with indentation. Consider the following (indentation is important): #+BEGIN_SRC emacs-lisp -n 20 ;; This exports with line number 20. (message "This is line 21") #+END_SRC After M-x org-babel-demarcate-block, I am getting #+BEGIN_SRC emacs-lisp -n 20 ;; This exports with line number 20. #+END_SRC #+begin_src emacs-lisp -n 20 (message "This is line 21") #+end_src --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-org-babel-demarcate-block-split-using-org-element-in.patch >From f5b9a6862cdb71ab33b7a291386221fff6648d53 Mon Sep 17 00:00:00 2001 Message-ID: From: Gerard Vermeulen Date: Sat, 30 Dec 2023 19:25:25 +0100 Subject: [PATCH] org-babel-demarcate-block: split using org-element instead of regexp * lisp/ob-babel.el (org-babel-demarcate-block): Delete the caption and the name from a copy of (org-element-at-point) and set its value to the body inside the source block after point. Delete all superfluous text after point from the current Emacs buffer and add a proper sentinel to the upper source block. Insert the lower block by applying `org-element-interpret-data' to the modified copy. Leave point in a convenient position. * testing/lisp/test-ob.el (test-ob/demarcate-block-split): New test for block splitting by demarcation. It checks also that the language, switches, and header arguments are duplicated. --- lisp/ob-core.el | 38 ++++++++++++++------------------------ testing/lisp/test-ob.el | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index f7e4e255f..300747dae 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -73,6 +73,7 @@ (declare-function org-element-contents-end "org-element" (node)) (declare-function org-element-parent "org-element-ast" (node)) (declare-function org-element-type "org-element-ast" (node &optional anonymous)) (declare-function org-element-type-p "org-element-ast" (node &optional types)) +(declare-function org-element-interpret-data "org-element" (data)) (declare-function org-entry-get "org" (pom property &optional inherit literal-nil)) (declare-function org-escape-code-in-region "org-src" (beg end)) (declare-function org-forward-heading-same-level "org" (arg &optional invisible-ok)) @@ -2067,35 +2068,24 @@ (defun org-babel-demarcate-block (&optional arg) (start (org-babel-where-is-src-block-head)) ;; `start' will be nil when within space lines after src block. (block (and start (match-string 0))) - (headers (and start (match-string 4))) (stars (concat (make-string (or (org-current-level) 1) ?*) " ")) (upper-case-p (and block (let (case-fold-search) (string-match-p "#\\+BEGIN_SRC" block))))) (if (and info start) ;; At src block, but not within blank lines after it. - (mapc - (lambda (place) - (save-excursion - (goto-char place) - (let ((lang (nth 0 info)) - (indent (make-string (org-current-text-indentation) ?\s))) - (when (string-match "^[[:space:]]*$" - (buffer-substring (line-beginning-position) - (line-end-position))) - (delete-region (line-beginning-position) (line-end-position))) - (insert (concat - (if (looking-at "^") "" "\n") - indent (if upper-case-p "#+END_SRC\n" "#+end_src\n") - (if arg stars indent) "\n" - indent (if upper-case-p "#+BEGIN_SRC " "#+begin_src ") - lang - (if (> (length headers) 1) - (concat " " headers) headers) - (if (looking-at "[\n\r]") - "" - (concat "\n" (make-string (current-column) ? ))))))) - (move-end-of-line 2)) - (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>)) + (let* ((body-end (match-end 5)) + (copy (org-element-copy (org-element-at-point))) + (end (org-element-end copy)) + (indent (make-string (org-current-text-indentation) ?\s))) + (org-element-put-property copy :caption nil) + (org-element-put-property copy :name nil) + (org-element-put-property copy :value (buffer-substring (point) body-end)) + (delete-region (point) end) + (insert (concat indent (if upper-case-p "#+END_SRC\n" "#+end_src\n") + (if arg stars indent) "\n")) + (insert (org-element-interpret-data copy)) + ;; Go back to the lower source block for `org-narrow-to-element': + (re-search-backward (rx bol (1+ nonl)))) (let ((start (point)) (lang (or (car info) ; Reuse language from previous block. (completing-read diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el index 42c77ca56..e57edfa22 100644 --- a/testing/lisp/test-ob.el +++ b/testing/lisp/test-ob.el @@ -26,6 +26,41 @@ (require 'ob-ref) (require 'org-table) (eval-and-compile (require 'cl-lib)) +(ert-deftest test-ob/demarcate-block-split () + "Test duplication of headers and switches in demarcation block splitting." + (org-test-with-temp-text " +#+header: :var edge=\"also duplicated\" +#+header: :wrap \"src any-spanish -n\" +#+begin_src emacs-lisp -i -n :var here=\"duplicated\" :wrap \"src any-english -n\" + +'above-split + +'below-split + +#+end_src +" + (let ((wrap-val "src any-spanish -n") above below avars bvars) + (org-babel-demarcate-block) + (setq above (org-babel-get-src-block-info)) + (setq avars (org-babel--get-vars (nth 2 above))) + (org-babel-next-src-block) + (setq below (org-babel-get-src-block-info)) + (setq bvars (org-babel--get-vars (nth 2 below))) + ;; duplicated multi-line header arguments: + (should (string= "also duplicated" (cdr (assq 'edge avars)))) + (should (string= "also duplicated" (cdr (assq 'edge bvars)))) + (should (string= wrap-val (cdr (assq :wrap (nth 2 above))))) + (should (string= wrap-val (cdr (assq :wrap (nth 2 below))))) + ;; duplicated language, other header arguments, and switches: + (should (string= "emacs-lisp" (nth 0 above))) + (should (string= "emacs-lisp" (nth 0 below))) + (should (string= "'above-split" (org-trim (nth 1 above)))) + (should (string= "'below-split" (org-trim (nth 1 below)))) + (should (string= "duplicated" (cdr (assq 'here avars)))) + (should (string= "duplicated" (cdr (assq 'here bvars)))) + (should (string= "-i -n" (nth 3 above))) + (should (string= "-i -n" (nth 3 below)))))) + (ert-deftest test-ob/indented-cached-org-bracket-link () "When the result of a source block is a cached indented link it should still return the link." -- 2.42.0 --=-=-= Content-Type: text/plain -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at --=-=-=--