all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [BUG] (org-element-context) caused (re-search-forward <regexp>) + (match-string-no-properties 1) returned matched data wrong
@ 2024-04-01 12:38 Christopher M. Miles
  2024-04-01 12:47 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Christopher M. Miles @ 2024-04-01 12:38 UTC (permalink / raw)
  To: Org mode


[-- Attachment #1.1: Type: text/plain, Size: 2644 bytes --]



Here is a simple test:

#+begin_src org :file "test.org"
,#+NAME: hello1
,#+BEGIN_SRC sh :var name=""
  echo "hello, $name"
,#+END_SRC

,#+BEGIN_SRC sh :noweb yes
<<hello1(name="chris")>>
,#+END_SRC

,#+NAME: hello2
,#+BEGIN_SRC emacs-lisp :var name="" :results output
(prin1 name)
,#+END_SRC

,#+BEGIN_SRC emacs-lisp :noweb yes :results output
<<hello2(name="chris")>>
,#+END_SRC

,#+BEGIN_SRC emacs-lisp :results output
(prin1 "hi")
,#+END_SRC

,#+RESULTS:
: "hi"

#+end_src

#+begin_src emacs-lisp :results output
(defvar org-simple-ref-label-re
  (rx-to-string
   '(group-n 1 (one-or-more (any word "-.:?!`'/*@+|(){}<>&_^$#%~"))))
  "Regexp for labels.")

(defvar org-simple-ref-ref-label-regexps
  (list
   (concat ":ID:\\s-+" org-simple-ref-label-re "\\_>")
   ;; CUSTOM_ID in a heading
   (concat ":CUSTOM_ID:\\s-+" org-simple-ref-label-re "\\_>")
   ;; #+name
   (concat "^\\s-*#\\+name:\\s-+" org-simple-ref-label-re)
   ;; labels in latex
   (concat "\\\\label{" org-simple-ref-label-re "}")
   ;; A target, code copied from org-target-regexp and group 1 numbered.
   (let ((border "[^<>\n\r \t]"))
     (format "<<\\(?1:%s\\|%s[^<>\n\r]*%s\\)>>" border border border))
   ;; A label link
   (concat "label:" org-simple-ref-label-re "\\_>")
   ;; code ref
   "[ 	]*(\\(?2:ref\\):\\(?1:[-a-zA-Z0-9_][-a-zA-Z0-9_ ]*\\))[ 	]*$"
   ;; noweb ref
   ":noweb-ref\\s-+\\(?1:.*?\\)\\s-"
   "\\\\lstset{.*label=\\(?1:.*?\\),.*}")
  "List of regular expressions to labels.
The label should always be in group 1.")

(with-current-buffer (get-buffer "test.org")
  (org-with-wide-buffer
   (goto-char (point-min))
   (while (re-search-forward (string-join org-simple-ref-ref-label-regexps "\\|") nil t)
     (save-match-data
       (print (format "%s -> %s" (match-string-no-properties 0) (match-string-no-properties 1)))

       (org-element-context) ; <----- REASON!! cause `match-string-no-properties' result wrong.

       (print (propertize (match-string-no-properties 1)
                          'org-simple-ref--location
                          (match-beginning 1)))))))
#+end_src

#+RESULTS[(2024-04-01 20:16:21) dc738972b5676dea6e0821cb02b2767219f4fd77]:
#+begin_example

"
,#+NAME: hello1 -> hello1"

#("NAME" 0 4 (org-simple-ref--location 46))

"<<hello1(name=\"chris\")>> -> hello1(name=\"chris\")"

#("hello1(name=\"chris\")" 0 20 (org-simple-ref--location 148))

"
,#+NAME: hello2 -> hello2"

#("NAME" 0 4 (org-simple-ref--location 184))

"<<hello2(name=\"chris\")>> -> hello2(name=\"chris\")"

#("hello2(name=\"chris\")" 0 20 (org-simple-ref--location 325))
#+end_example

Found reason, check out the marker in the screenshot:


[-- Attachment #1.2: Screenshot 2024-04-01 at 20.25.43.png --]
[-- Type: image/png, Size: 1012242 bytes --]

[-- Attachment #1.3: Type: text/plain, Size: 397 bytes --]


Get more background details from this link: https://github.com/Elilif/Elilif.github.io/discussions/8#discussioncomment-8971127

-- 

[ stardiviner ]
I try to make every word tell the meaning that I want to express without misunderstanding.

Blog: https://stardiviner.github.io/
IRC(libera.chat, freenode): stardiviner, Matrix: stardiviner
GPG: F09F650D7D674819892591401B5DF1C95AE89AC3

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: [BUG] (org-element-context) caused (re-search-forward <regexp>) + (match-string-no-properties 1) returned matched data wrong
  2024-04-01 12:38 [BUG] (org-element-context) caused (re-search-forward <regexp>) + (match-string-no-properties 1) returned matched data wrong Christopher M. Miles
@ 2024-04-01 12:47 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2024-04-01 12:47 UTC (permalink / raw)
  To: Christopher M. Miles; +Cc: Org mode

"Christopher M. Miles" <numbchild@gmail.com> writes:

> Re: [BUG] (org-element-context) caused (re-search-forward <regexp>) + (match-string-no-properties 1) returned matched data wrong

`org-element-context' does not guarantee that match data is not altered.
You need to wrap it into `save-match-data' if necessary.
<help:org-element-context>:

    org-element-context is an autoloaded and natively compiled function
    defined in org-element.el.
    
    ...
    This function may modify match data.

Not a bug.
Canceled.

-- 
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] 2+ messages in thread

end of thread, other threads:[~2024-04-01 12:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-01 12:38 [BUG] (org-element-context) caused (re-search-forward <regexp>) + (match-string-no-properties 1) returned matched data wrong Christopher M. Miles
2024-04-01 12:47 ` Ihor Radchenko

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.