org-babel-demarcate-block: Fix when within blank after src block

* lisp/ob-core.el (org-babel-where-is-src-block-head): Clarify that
return value is nil within blank after src block.
(org-babel-demarcate-block): Insert new block inheriting language when
within blank lines after existing src block.  Document the behavior.

Reported-by: dalanicolai <dalanicolai@gmail.com>
Link: https://orgmode.org/list/CACJP=3nCekgX4RuYBWf=R_2t05-s4ikx-L9qP7W0cSicNG55BA@mail.gmail.com
This commit is contained in:
Ihor Radchenko 2022-10-25 17:38:35 +08:00
parent c3aa6a6d43
commit cd835dcad8
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 23 additions and 17 deletions

View File

@ -1813,7 +1813,8 @@ its current beginning instead.
Return the point at the beginning of the current source block.
Specifically at the beginning of the #+BEGIN_SRC line. Also set
match-data relatively to `org-babel-src-block-regexp', which see.
If the point is not on a source block then return nil."
If the point is not on a source block or within blank lines after an
src block, then return nil."
(let ((element (or src-block (org-element-at-point))))
(when (eq (org-element-type element) 'src-block)
(let ((end (org-element-property :end element)))
@ -1965,17 +1966,21 @@ With optional prefix argument ARG, jump backward ARG many source blocks."
When called from inside of a code block the current block is
split. When called from outside of a code block a new code block
is created. In both cases if the region is demarcated and if the
region is not active then the point is demarcated."
region is not active then the point is demarcated.
When called within blank lines after a code block, create a new code
block of the same language with the previous."
(interactive "P")
(let* ((info (org-babel-get-src-block-info 'no-eval))
(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 info
(if (and info start) ;; At src block, but not within blank lines after it.
(mapc
(lambda (place)
(save-excursion
@ -1987,26 +1992,27 @@ region is not active then the point is demarcated."
(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)
(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]")
(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 ((start (point))
(lang (completing-read
"Lang: "
(mapcar #'symbol-name
(delete-dups
(append (mapcar #'car org-babel-load-languages)
(mapcar (lambda (el) (intern (car el)))
org-src-lang-modes))))))
(lang (or (car info) ; Reuse language from previous block.
(completing-read
"Lang: "
(mapcar #'symbol-name
(delete-dups
(append (mapcar #'car org-babel-load-languages)
(mapcar (lambda (el) (intern (car el)))
org-src-lang-modes)))))))
(body (delete-and-extract-region
(if (org-region-active-p) (mark) (point)) (point))))
(insert (concat (if (looking-at "^") "" "\n")