emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Bug] org-in-src-block-p returns t when not in source block
@ 2023-11-27 15:49 No Wayman
  2023-11-27 15:55 ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: No Wayman @ 2023-11-27 15:49 UTC (permalink / raw)
  To: emacs-orgmode


When on an empty line before a source block, org-in-src-block-p 
correctly returns nil.
However, when on an empty line after a source block, it returns t.
This is reproducible on my end with both the built-in version of 
Org and 9.7-pre. 

Yodel[1] Report 2023-11-27 10:42:54
===================================

--8<---------------cut here---------------start------------->8---
(yodel
  :post*
  (require 'org)
  (org-version nil 'full 'message)
  (yodel-file
    :point "|"
    :with* "#+begin_src text\n#+end_src\n|"
    :then* (message "org-in-src-block-p below: %s" 
    (org-in-src-block-p)))
  (yodel-file
    :point "|"
    :with* "|\n#+begin_src text\n#+end_src"
    :then* (message "org-in-src-block-p above: %s" 
    (org-in-src-block-p))))
--8<---------------cut here---------------end--------------->8---

STDOUT
======

> Org mode version 9.6.11 (release_9.6.11 @ 
> /usr/share/emacs/30.0.50/lisp/org/)
> org-in-src-block-p below: t
> org-in-src-block-p above: nil

Environment
===========

- emacs version: GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, 
  GTK+ Version 3.24.38, cairo version 1.18.0)
 of 2023-11-16
- system type: gnu/linux

[1] https://www.github.com/progfolio/yodel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Bug] org-in-src-block-p returns t when not in source block
  2023-11-27 15:49 No Wayman
@ 2023-11-27 15:55 ` Ihor Radchenko
       [not found]   ` <87cyvv9nag.fsf@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2023-11-27 15:55 UTC (permalink / raw)
  To: No Wayman; +Cc: emacs-orgmode

No Wayman <iarchivedmywholelife@gmail.com> writes:

> When on an empty line before a source block, org-in-src-block-p 
> correctly returns nil.
> However, when on an empty line after a source block, it returns t.
> This is reproducible on my end with both the built-in version of 
> Org and 9.7-pre. 

This is not a bug - Org considers blank lines after element as a part of
it. This includes source blocks.

If you want to check if point is inside source block code, use INSIDE
argument.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Bug] org-in-src-block-p returns t when not in source block
       [not found]   ` <87cyvv9nag.fsf@gmail.com>
@ 2023-11-27 19:59     ` Ihor Radchenko
  0 siblings, 0 replies; 8+ messages in thread
From: Ihor Radchenko @ 2023-11-27 19:59 UTC (permalink / raw)
  To: No Wayman; +Cc: emacs-orgmode

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

[ Adding Org mailing list back to CC ]

No Wayman <iarchivedmywholelife@gmail.com> writes:

>> This is not a bug - Org considers blank lines after element as a 
>> part of
>> it. This includes source blocks.
>
> I would've reversed the semantics of the INSIDE argument.
> e.g.
>
> (defun org-in-src-block-p (&optional lax element)
> ...
> That's less confusing and mirrors other functions, such as 
> org-at-timestamp-p.

This is very different from LAX argument in `org-at-timestamp-p' -
timestamp-like text may not be proper syntactically correct Org markup
element, but is still considered by agenda for historical and practical
reasons.

> However, that's a breaking change, and I feel like adding another, 
> similarly named function will cause more confusion than it 
> prevents.

This too.

> ... With that in mind, the docstring could be improved.
> ...

What about the attached patch?


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-org.el-org-in-src-block-p-Improve-docstring.patch --]
[-- Type: text/x-patch, Size: 1374 bytes --]

From e5d5ab915eb4309082a4a274170fddc60a75c28b Mon Sep 17 00:00:00 2001
Message-ID: <e5d5ab915eb4309082a4a274170fddc60a75c28b.1701114969.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Mon, 27 Nov 2023 20:54:42 +0100
Subject: [PATCH] lisp/org.el (org-in-src-block-p): Improve docstring

* lisp/org.el (org-in-src-block-p):

Reported-by: No Wayman <iarchivedmywholelife@gmail.com>
Link: https://orgmode.org/list/87il5n9owq.fsf@gmail.com
---
 lisp/org.el | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index d2cd0b9b7..19887ff1e 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -18786,9 +18786,13 @@ (defun org-quote-vert (s)
   s)
 
 (defun org-in-src-block-p (&optional inside element)
-  "Whether point is in a code source block.
-When INSIDE is non-nil, don't consider we are within a source
-block when point is at #+BEGIN_SRC or #+END_SRC.
+  "Return t when point is at a source block element.
+When INSIDE is non-nil, return t only when point is between #+BEGIN_SRC
+and #+END_SRC lines.
+
+Note that affiliated keywords and blank lines after are considered a
+part of a source block.
+
 When ELEMENT is provided, it is considered to be element at point."
   (save-match-data (setq element (or element (org-element-at-point))))
   (when (org-element-type-p element 'src-block)
-- 
2.42.0


[-- Attachment #3: Type: text/plain, Size: 224 bytes --]


-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [Bug] org-in-src-block-p returns t when not in source block
@ 2023-11-27 20:14 No Wayman
  2023-11-27 20:30 ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: No Wayman @ 2023-11-27 20:14 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


[Apologies for the duplicate message, Ihor. mu4e recently changed it's default
reply behavior without my noticing]

Ihor Radchenko <yantar92@posteo.net> writes:

> This is very different from LAX argument in `org-at-timestamp-p' -
> timestamp-like text may not be proper syntactically correct Org markup
> element, but is still considered by agenda for historical and practical
> reasons.

Fair enough.
 
> What about the attached patch?

That is much clearer. Thank you.

> If you want to check if point is inside source block code, use INSIDE
> argument.

Using the INSIDE argument still appears to fail in some cases:

Yodel[1] Report 2023-11-27 14:55:05
===================================

--8<---------------cut here---------------start------------->8---
(yodel
  :post*
  (require 'org)
  (org-version nil 'full 'message)
  (message "(org-in-source-block-p 'inside)")
  (yodel-file "before.org" :point "|"
    :with* "|\n#+begin_src text\ntest\n#+end_src\n"
    :then* (message "point on preceding blank line: %s"
             (if (org-in-src-block-p 'inside) "FAIL" "PASS")))
  (yodel-file "begin.org" :point "|"
    :with* "\n#+begin|_src text\ntest\n#+end_src\n"
    :then* (message "point on begin_src: %s"
             (if (org-in-src-block-p 'inside) "FAIL" "PASS")))
  (yodel-file "in.org" :point "|"
    :with* "\n#+begin_src text\nte|st\n#+end_src\n"
    :then* (message "point between begin_src end_src: %s"
             (if (org-in-src-block-p 'inside) "PASS" "FAIL")))
  (yodel-file "end-non-blank.org" :point "|"
    :with* "\n#+begin_src text\ntest\n#+end|_src\ntest"
    :then* (message "point on end_src (non-blank line after): %s"
             (if (org-in-src-block-p 'inside) "FAIL" "PASS")))
  (yodel-file "end.org" :point "|"
    :with* "\n#+begin_src text\ntest\n#+end|_src\n"
    :then* (message "point on end_src (blank line after): %s"
             (if (org-in-src-block-p 'inside) "FAIL" "PASS")))
  (yodel-file "end-eobp.org" :point "|"
    :with* "\n#+begin_src text\ntest\n#+end|_src"
    :then* (message "point on end_src (no line after): %s"
             (if (org-in-src-block-p 'inside) "FAIL" "PASS")))
  (yodel-file "after.org" :point "|"
    :with* "\n#+begin_src text\ntest\n#+end_src\n|"
    :then* (message "point on trailing blank line: %s"
                    (if (org-in-src-block-p 'inside) "FAIL" "PASS"))))
--8<---------------cut here---------------end--------------->8---

STDOUT
======

> Org mode version 9.6.11 (release_9.6.11 @ /usr/share/emacs/30.0.50/lisp/org/)
> (org-in-source-block-p ’inside)
> point on preceding blank line: PASS
> point on begin_src: PASS
> point between begin_src end_src: PASS
> point on end_src (non-blank line after): PASS
> point on end_src (blank line after): PASS
> point on end_src (no line after): FAIL
> point on trailing blank line: FAIL

Environment
===========

- emacs version: GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.38, cairo version 1.18.0)
 of 2023-11-16
- system type: gnu/linux

[1] https://www.github.com/progfolio/yodel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Bug] org-in-src-block-p returns t when not in source block
  2023-11-27 20:14 [Bug] org-in-src-block-p returns t when not in source block No Wayman
@ 2023-11-27 20:30 ` Ihor Radchenko
  2023-11-27 20:39   ` No Wayman
  2023-11-28 20:32   ` Jens Schmidt
  0 siblings, 2 replies; 8+ messages in thread
From: Ihor Radchenko @ 2023-11-27 20:30 UTC (permalink / raw)
  To: No Wayman; +Cc: emacs-orgmode

No Wayman <iarchivedmywholelife@gmail.com> writes:

>> What about the attached patch?
>
> That is much clearer. Thank you.

Applied, onto main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=bd7fab212

>> If you want to check if point is inside source block code, use INSIDE
>> argument.
>
> Using the INSIDE argument still appears to fail in some cases:
>
> ...
>> Org mode version 9.6.11 (release_9.6.11 @ /usr/share/emacs/30.0.50/lisp/org/)
>> (org-in-source-block-p ’inside)
>> point on preceding blank line: PASS
>> point on begin_src: PASS
>> point between begin_src end_src: PASS
>> point on end_src (non-blank line after): PASS
>> point on end_src (blank line after): PASS
>> point on end_src (no line after): FAIL

This one passed on main.

>> point on trailing blank line: FAIL

Fixed, on main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6d7c7917a

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Bug] org-in-src-block-p returns t when not in source block
  2023-11-27 20:30 ` Ihor Radchenko
@ 2023-11-27 20:39   ` No Wayman
  2023-11-28 20:32   ` Jens Schmidt
  1 sibling, 0 replies; 8+ messages in thread
From: No Wayman @ 2023-11-27 20:39 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

>>> point on trailing blank line: FAIL
>
> Fixed, on main.
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6d7c7917a

Confirmed on my end by adding `:packages* org` to the previous 
test:

Yodel[1] Report 2023-11-27 15:34:45
===================================

STDOUT
======

> INFO     Scraping files for loaddefs... 
>   INFO     Scraping files for loaddefs...done
>   GEN      ../elpaca-autoloads.el
> Cloning into '/tmp/yodel-yCfbwq/elpaca/repos/elpaca'...
> Your branch is up to date with 'origin/master'.
> Checking /tmp/yodel-yCfbwq/elpaca/repos/elpaca...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-info.el...
> Compiling /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-log.el...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-manager.el...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-menu-elpa.el...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-menu-melpa.el...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-menu-org.el...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-process.el...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-test.el...
> Compiling /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca-ui.el...
> Compiling /tmp/yodel-yCfbwq/elpaca/repos/elpaca/elpaca.el...
> Checking /tmp/yodel-yCfbwq/elpaca/repos/elpaca/doc...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/doc/early-init.el...
> Compiling /tmp/yodel-yCfbwq/elpaca/repos/elpaca/doc/init.el...
> Checking /tmp/yodel-yCfbwq/elpaca/repos/elpaca/extensions...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/extensions/elpaca-use-package.el...
> Checking /tmp/yodel-yCfbwq/elpaca/repos/elpaca/images...
> Checking /tmp/yodel-yCfbwq/elpaca/repos/elpaca/test...
> Compiling 
> /tmp/yodel-yCfbwq/elpaca/repos/elpaca/test/elpaca-test.el...
> Done (Total of 11 files compiled, 3 skipped in 4 directories)

> Downloading MELPA recipes...
> Downloading MELPA recipes...100%
> Downloading NonGNU-devel ELPA...
> Downloading GNU-devel ELPA...
> Downloading NonGNU ELPA...
> Downloading GNU ELPA...
> Org mode version 9.7-pre (9.7-pre-n/a-g6d7c79
>  @ /tmp/yodel-yCfbwq/elpaca/builds/org/)
> (org-in-source-block-p ’inside)
> point on preceding blank line: PASS
> point on begin_src: PASS
> point between begin_src end_src: PASS
> point on end_src (non-blank line after): PASS
> point on end_src (blank line after): PASS
> point on end_src (no line after): PASS
> point on trailing blank line: PASS

Environment
===========

- emacs version: GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, 
  GTK+ Version 3.24.38, cairo version 1.18.0)
 of 2023-11-16
- system type: gnu/linux

Packages
========

- elpaca 
  https://github.com/progfolio/elpaca/commit/86a562c0a7c0a009803172eb0ed55fdaf17c4559
- org 
  https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6d7c7917a0294bb71c3294119984207b1d9b0385

[1] https://www.github.com/progfolio/yodel



Thanks again.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Bug] org-in-src-block-p returns t when not in source block
  2023-11-27 20:30 ` Ihor Radchenko
  2023-11-27 20:39   ` No Wayman
@ 2023-11-28 20:32   ` Jens Schmidt
  2023-11-29 10:07     ` Ihor Radchenko
  1 sibling, 1 reply; 8+ messages in thread
From: Jens Schmidt @ 2023-11-28 20:32 UTC (permalink / raw)
  To: Ihor Radchenko, No Wayman; +Cc: emacs-orgmode

On 2023-11-27  21:30, Ihor Radchenko wrote:

> Fixed, on main.
> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6d7c7917a

Which is currently my HEAD, and there are still edge cases that do not
seem to be handled well.

Start off from "make vanilla" and the following buffer

------------------------- test.org -------------------------
#+begin_src shell
foo
#+end_src

bar
------------------------- test.org -------------------------

Navigate to BOL of the "#+end_src" line and

  M-: (org-in-src-block-p t)
  => nil

As expected.  Now add a space on the empty line *after* the
"#+end_src" line, navigate back to BOL of the "#+end_src" line and

  M-: (org-in-src-block-p t)
  => t

When I wrote my own inner-boundary function, I looked with envy at
the elegance of the arithmetic done in `org-in-src-block-p' but thought
that this probably would not always work ... or did I misunderstand
something here?

Thanks								Jens

Here is the lossage corresponding to my test case, just in case:

 C-s	  ;; isearch-forward
 #	  ;; isearch-printing-char
 +	  ;; isearch-printing-char
 e	  ;; isearch-printing-char
 n	  ;; isearch-printing-char
 d	  ;; isearch-printing-char
 C-a	  ;; org-beginning-of-line
 M-: (org-in-src-block-p t)
 <return> ;; read--expression-try-read
 C-n	  ;; next-line
 SPC	  ;; org-self-insert-command
 C-a	  ;; org-beginning-of-line
 C-p	  ;; previous-line
 M-: (org-in-src-block-p t)
 <return> ;; read--expression-try-read


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Bug] org-in-src-block-p returns t when not in source block
  2023-11-28 20:32   ` Jens Schmidt
@ 2023-11-29 10:07     ` Ihor Radchenko
  0 siblings, 0 replies; 8+ messages in thread
From: Ihor Radchenko @ 2023-11-29 10:07 UTC (permalink / raw)
  To: Jens Schmidt; +Cc: No Wayman, emacs-orgmode

Jens Schmidt <jschmidt4gnu@vodafonemail.de> writes:

> On 2023-11-27  21:30, Ihor Radchenko wrote:
>
>> Fixed, on main.
>> https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6d7c7917a
>
> Which is currently my HEAD, and there are still edge cases that do not
> seem to be handled well.
> ...
> When I wrote my own inner-boundary function, I looked with envy at
> the elegance of the arithmetic done in `org-in-src-block-p' but thought
> that this probably would not always work ... or did I misunderstand
> something here?

You are right.
Fixed, on main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=57b94f344

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-11-29 10:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-27 20:14 [Bug] org-in-src-block-p returns t when not in source block No Wayman
2023-11-27 20:30 ` Ihor Radchenko
2023-11-27 20:39   ` No Wayman
2023-11-28 20:32   ` Jens Schmidt
2023-11-29 10:07     ` Ihor Radchenko
  -- strict thread matches above, loose matches on Subject: below --
2023-11-27 15:49 No Wayman
2023-11-27 15:55 ` Ihor Radchenko
     [not found]   ` <87cyvv9nag.fsf@gmail.com>
2023-11-27 19:59     ` Ihor Radchenko

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).