emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: gerard.vermeulen@posteo.net
To: Ihor Radchenko <yantar92@posteo.net>
Cc: Emacs orgmode <emacs-orgmode@gnu.org>,
	emacs-orgmode-bounces+gerard.vermeulen=posteo.net@gnu.org
Subject: Re: [PATCH] org-babel-demarcate-block: split using element API
Date: Mon, 26 Feb 2024 08:51:22 +0000	[thread overview]
Message-ID: <529eafb5a5274fb4e15917ce3b97d785@posteo.net> (raw)
In-Reply-To: <87y1b8loop.fsf@localhost>

[-- Attachment #1: Type: text/plain, Size: 2356 bytes --]



On 25.02.2024 13:21, Ihor Radchenko wrote:
> gerard.vermeulen@posteo.net writes:
> 
>> I added the caveat:
>> This patch is incompatible with `org-babel-edit-prep:<LANG>' functions
>> that signal `user-error's.
>> to the commit message and cleaned it up a bit.
> 
> You may wrap `org-indent-block' into `condition-case' to catch
> user-errors.

The caveat is not a real constraint, since Org has limited support for
source block editing in an Org mode buffer when an
`org-babel-edit-prep:<LANG>' function signals an user-error.

I show that in the attached no-user-errors-in-edit-prep.org.

I also attach condition-case.diff that I use to try to argue that
condition-case never gets into its error-handler (I am running the
code with the condition-case now), because I never see the signal of
~(user-error "Error in `org-babel-edit-prep:<LANG>'?")~ (I needed to
read the condition-case documentation, because this is the first time
I am trying to use it).

#+begin_src emacs-lisp -n :results silent
;; Fails with org-babel-demarcate-block in patch.
(defun edit-prep-user-error (_info)
   (user-error "Signaling user-errors is harmfull"))

(defun org-babel-edit-prep:python (info)
   (edit-prep-user-error info))

(message "Harm-FULL edit-prep:python")
#+end_src

#+begin_src emacs-lisp -n :results silent
;; Works with org-babel-demarcate-block in patch.
(defun edit-prep-message (_info)
   (message "Displaying messages is harmless"))

(defun org-babel-edit-prep:python (info)
   (edit-prep-message info))

(message "Harm-LESS edit-prep:python")
#+end_src

~org-babel-demarcate-blocks~ works with "Harm-LESS edit-prep:python"
but fails with "Harm-FULL edit-prep:python" and it leaves the Org mode
buffer in a state similar to shown in no-user-errors-in-edit-prep.org
(I have to pass through org-edit-src-code to be able to edit the block
in the Org mode buffer).

But I only can do that after interrupting (C-g C-g) an infinite stream
of warnings "Warning (org-element): ‘org-element-at-point’ cannot be
used in non-Org buffer #<buffer *Org Src mail.org[ python ]*>
(python-mode)" which should come from the `org-element-at-point' call
indicated by ;; <= HERE? in condition-case.diff.

Test block below:

#+begin_src python -i -n :results silent
11
22
#+end_src

Regards -- Gerard

[-- Attachment #2: no-user-errors-in-edit-prep.org --]
[-- Type: application/octet-stream, Size: 2281 bytes --]

The caveat is not a real constraint, since Org has limited support for
source block editing in an Org mode buffer when an
`org-babel-edit-prep:<LANG>' function signals an user-error.  Demo:

#+begin_src emacs-lisp -n :results silent
;; Limited source block editing in an Org buffer.
(defun edit-prep-user-error (_info)
  (user-error "Signaling user-errors is harmfull"))

(defun org-babel-edit-prep:python (info)
  (edit-prep-user-error info))
#+end_src

#+begin_src emacs-lisp -n :results silent
;; Supports source block editing in an Org buffer.
(defun edit-prep-message (_info)
  (message "Displaying messages is harmless"))

(defun org-babel-edit-prep:python (info)
  (edit-prep-message info))
#+end_src

When org-babel-edit-prep:python displays a message and without using
M-x org-edit-src-code, it is possible to insert newlines like any
other character in the block below:

#+begin_src python -i -n :results silent
# comment
#+end_src

But when org-babel-edit-prep:python signals an user-error, inserting a
newline differs from inserting other characters:
1. The newline gets inserted but the edit buffer changes state (turns
   yellow on my system)
2. Typing any character does not work, but I get in the message buffer:
   - "Cannot modify an area being edited in a dedicated buffer"
3. After M-x org-edit-src-code I get in the message buffer:
   - "Return to existing edit buffer ([n] will revert changes)? (y or n) y"
   - "edit-prep-user-error: Signaling user-errors is harmfull"
4. It is possible to go back immediately to the Org mode buffer to
   insert a character sequence (terminated by a newline that changes
   the state of the edit buffer back to 1).
When org-babel-edit-prep:python displays a message, I never see
turning the state of an Org source buffer to "dedicated" when editing
the block in an Org mode buffer.






I am sure that the "dedicated" state of the Org source buffer is
related to the warnings: "Warning (org-element):
‘org-element-at-point’ cannot be used in non-Org buffer #<buffer *Org
Src no-user-errors-in-edit-prep.org[ python ]*> (python-mode)" when I
try to split the blocks by demarcation with the patch.

#+begin_src emacs-lisp -n :results silent
(condition-case nil
    (user-error "Hidden")
  (user-error "Shown"))
#+end_src

[-- Attachment #3: condition-case.diff --]
[-- Type: application/octet-stream, Size: 7440 bytes --]

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index e3110a3f1..c2e9bf97a 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2101,63 +2101,65 @@ block of the same language as the previous."
 			    (let (case-fold-search)
 			      (string-match-p "#\\+BEGIN_SRC" block)))))
     (if (and info start) ;; At src block, but not within blank lines after it.
-        (let* ((copy (org-element-copy (org-element-at-point)))
-               (before (org-element-begin copy))
-               (beyond (org-element-end copy))
-               (parts
-                (if (org-region-active-p)
-                    (list body-beg (region-beginning) (region-end) body-end)
-                  (list body-beg (point) body-end)))
-               (pads ;; To calculate left-side white-space padding.
-                (if (org-region-active-p)
-                    (list (region-beginning) (region-end))
-                  (list (point))))
-               (n (- (length parts) 2)) ;; 1 or 2 parts in `dolist' below.
-               ;; `post-blank' caches the property before setting it to 0.
-               (post-blank (org-element-property :post-blank copy)))
-          ;; Point or region are within body when parts is in increasing order.
-          (unless (apply #'<= parts)
-            (user-error "Select within the source block body to split it"))
-          (setq parts (mapcar (lambda (p) (buffer-substring (car p) (cdr p)))
-                              (seq-mapn #'cons parts (cdr parts))))
-          ;; Map positions to columns for white-space padding.
-          (setq pads (mapcar (lambda (p) (save-excursion
-                                           (goto-char p)
-                                           (current-column)))
-                             pads))
-          (push 0 pads) ;; The 1st part never requires white-space padding.
-          (setq parts (mapcar (lambda (p) (string-join
-                                           (list (make-string (car p) ?\s)
-                                                 (cdr p))))
-                              (seq-mapn #'cons pads parts)))
-          (delete-region before beyond)
-          ;; Set `:post-blank' to 0.  We take care of spacing between blocks.
-          (org-element-put-property copy :post-blank 0)
-          (org-element-put-property copy :value (car parts))
-          (insert (org-element-interpret-data copy))
-          ;; `org-indent-block' may see another `org-element' (e.g. paragraph)
-          ;; immediately after the block.  Ensure to indent the inserted block
-          ;; and move point to its end.
-          (org-babel-previous-src-block 1)
-          (org-indent-block)
-          (goto-char (org-element-end (org-element-at-point)))
-          (org-element-put-property copy :caption nil)
-          (org-element-put-property copy :name nil)
-          ;; Insert the 2nd block, and the 3rd block when region is active.
-          (dolist (part (cdr parts))
-            (org-element-put-property copy :value part)
-            (insert (if arg (concat stars "\n") "\n"))
-            (cl-decf n)
-            (when (= n 0)
-              ;; Use `post-blank' to reset the property of the last block.
-              (org-element-put-property copy :post-blank post-blank))
-            (insert (org-element-interpret-data copy))
-            ;; Ensure to indent the inserted block and move point to its end.
-            (org-babel-previous-src-block 1)
-            (org-indent-block)
-            (goto-char (org-element-end (org-element-at-point))))
-          ;; Leave point at the last inserted block.
-          (goto-char (org-babel-previous-src-block 1)))
+        (condition-case nil
+            (let* ((copy (org-element-copy (org-element-at-point))) ;; <= HERE?
+                   (before (org-element-begin copy))
+                   (beyond (org-element-end copy))
+                   (parts
+                    (if (org-region-active-p)
+                        (list body-beg (region-beginning) (region-end) body-end)
+                      (list body-beg (point) body-end)))
+                   (pads ;; To calculate left-side white-space padding.
+                    (if (org-region-active-p)
+                        (list (region-beginning) (region-end))
+                      (list (point))))
+                   (n (- (length parts) 2)) ;; 1 or 2 parts in `dolist' below.
+                   ;; `post-blank' caches the property before setting it to 0.
+                   (post-blank (org-element-property :post-blank copy)))
+              ;; Point or region are within body when parts is in increasing order.
+              (unless (apply #'<= parts)
+                (user-error "Select within the source block body to split it"))
+              (setq parts (mapcar (lambda (p) (buffer-substring (car p) (cdr p)))
+                                  (seq-mapn #'cons parts (cdr parts))))
+              ;; Map positions to columns for white-space padding.
+              (setq pads (mapcar (lambda (p) (save-excursion
+                                               (goto-char p)
+                                               (current-column)))
+                                 pads))
+              (push 0 pads) ;; The 1st part never requires white-space padding.
+              (setq parts (mapcar (lambda (p) (string-join
+                                               (list (make-string (car p) ?\s)
+                                                     (cdr p))))
+                                  (seq-mapn #'cons pads parts)))
+              (delete-region before beyond)
+              ;; Set `:post-blank' to 0.  We take care of spacing between blocks.
+              (org-element-put-property copy :post-blank 0)
+              (org-element-put-property copy :value (car parts))
+              (insert (org-element-interpret-data copy))
+              ;; `org-indent-block' may see another `org-element' (e.g. paragraph)
+              ;; immediately after the block.  Ensure to indent the inserted block
+              ;; and move point to its end.
+              (org-babel-previous-src-block 1)
+              (org-indent-block)
+              (goto-char (org-element-end (org-element-at-point)))
+              (org-element-put-property copy :caption nil)
+              (org-element-put-property copy :name nil)
+              ;; Insert the 2nd block, and the 3rd block when region is active.
+              (dolist (part (cdr parts))
+                (org-element-put-property copy :value part)
+                (insert (if arg (concat stars "\n") "\n"))
+                (cl-decf n)
+                (when (= n 0)
+                  ;; Use `post-blank' to reset the property of the last block.
+                  (org-element-put-property copy :post-blank post-blank))
+                (insert (org-element-interpret-data copy))
+                ;; Ensure to indent the inserted block and move point to its end.
+                (org-babel-previous-src-block 1)
+                (org-indent-block)
+                (goto-char (org-element-end (org-element-at-point))))
+              ;; Leave point at the last inserted block.
+              (goto-char (org-babel-previous-src-block 1)))
+          (user-error "Error in `org-babel-edit-prep:<LANG>'?"))
       (let ((start (point))
 	    (lang (or (car info) ; Reuse language from previous block.
                       (completing-read

  reply	other threads:[~2024-02-26  8:52 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-30 19:13 [PATCH] org-babel-demarcate-block: duplicate switches too gerard.vermeulen
2023-12-31 14:28 ` Ihor Radchenko
2024-01-01 12:52   ` gerard.vermeulen
2024-01-02 10:48     ` Ihor Radchenko
2024-01-02 20:20       ` [PATCH] org-babel-demarcate-block: split using org-element instead of regexp gerard.vermeulen
2024-01-03 15:11         ` Ihor Radchenko
2024-01-04  8:59           ` gerard.vermeulen
2024-01-04 14:43             ` Ihor Radchenko
2024-01-07 18:49               ` [PATCH] org-babel-demarcate-block: split using element API gerard.vermeulen
2024-01-08 12:08                 ` Ihor Radchenko
2024-01-08 20:25                   ` gerard.vermeulen
2024-01-09  7:49                     ` gerard.vermeulen
2024-01-09 10:50                       ` gerard.vermeulen
2024-01-09 14:49                         ` Ihor Radchenko
2024-01-13 14:04                           ` gerard.vermeulen
2024-01-13 15:17                             ` Ihor Radchenko
2024-01-13 20:16                               ` gerard.vermeulen
2024-01-14 10:53                                 ` gerard.vermeulen
2024-01-14 12:16                                   ` Ihor Radchenko
2024-01-14 19:18                                     ` gerard.vermeulen
2024-01-15  9:37                                       ` gerard.vermeulen
2024-01-16 13:34                                         ` Ihor Radchenko
2024-02-19  9:46                                           ` Ihor Radchenko
2024-02-19 13:01                                             ` gerard.vermeulen
2024-02-21  9:40                                               ` Ihor Radchenko
2024-02-21 18:19                                                 ` gerard.vermeulen
2024-02-22 16:28                                                   ` gerard.vermeulen
2024-02-23 13:43                                                     ` Ihor Radchenko
2024-02-25 12:06                                                       ` gerard.vermeulen
2024-02-25 12:21                                                         ` Ihor Radchenko
2024-02-26  8:51                                                           ` gerard.vermeulen [this message]
2024-02-28 11:54                                                             ` Ihor Radchenko
2024-02-29  9:50                                                               ` gerard.vermeulen
2024-02-29 11:56                                                                 ` Ihor Radchenko
2024-02-29 17:33                                                                   ` gerard.vermeulen
2024-03-03 13:08                                                                     ` Ihor Radchenko
2024-03-03 15:45                                                                       ` gerard.vermeulen
2024-03-04 10:12                                                                         ` Ihor Radchenko
2024-03-04 11:40                                                                           ` gerard.vermeulen
2024-03-04 11:51                                                                             ` Ihor Radchenko
2024-02-26  9:06                                                           ` gerard.vermeulen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=529eafb5a5274fb4e15917ce3b97d785@posteo.net \
    --to=gerard.vermeulen@posteo.net \
    --cc=emacs-orgmode-bounces+gerard.vermeulen=posteo.net@gnu.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).