Re: [FR] :noweb-wrap header arg

2024-07-01 Thread Ihor Radchenko
Ihor Radchenko  writes:

>> Let me know what you think!
>
> Consider cases like
> ...

It has been over a month since the last message in this thread.
Amy, may I know if you are still working on the patch?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [BUG] "Stack overflow in regexp matcher" when tangling or noweb referencing large blocks [9.7.4 (9.7.4-1387e3 @ /home/andrea/.emacs.d/elpa/org-9.7.4/)]

2024-06-16 Thread Ihor Radchenko
Andrea  writes:

> Thanks again for Org Mode!
> I recently moved to Org Mode 9.7 and I have a boring bug to share:
> on tangling or :noweb yes <> src blocks, I get "Stack
> overflow in regexp matcher".
>
> This was working with 9.6, maybe the regexp changed in a faulty way?

The change was very small:
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=eaf274909

>Debugger entered--Lisp error: (error "Stack overflow in regexp matcher")
>  org-babel-where-is-src-block-head()
>  org-babel-tangle((4))
>  funcall-interactively(org-babel-tangle (4))
>  command-execute(org-babel-tangle)
> ...
>Debugger entered--Lisp error: (error "Stack overflow in regexp matcher")
>  re-search-forward("^[ \11]*#\\+name:[ \11]*xx[ \11]*\\(?:\n[ 
> \11]*#\\+\\S-+:.*\\)*?..." nil t)

Confirmed.

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



Re: [FR] :noweb-wrap header arg

2024-05-23 Thread Ihor Radchenko
Amy Grinn  writes:

>> I recommend the following:
>>
>> If the value starts from ", use Elisp's `read':
>>|"# <<" ">>"
>>(read (current-buffer)) ; => "# <<"
>>otherwise, consider read until the first whitespace.
>>|#<<; >>;
>>(re-search-forward (rx (1+ (not whitespace
>>#<<;|
>>
>> However, there may be edge cases like
>>
>> "<< >>"
>> "<< >>
>> << << >>
>> << "asd" >>
>
> I didn't implement this recommendation yet; I still think the regex is a
> more clear way of putting it, even without using a temporary buffer:
>
> ;; If a pair of " is found separated by one or more
> ;; characters, capture those characters as a group
> (unless (eq i (string-match (rx (* space) ?\"
> (group (+ (not ?\")))
> ?\" (* space))
> raw i))
>   ;; Otherwise, capture the next non-whitespace group of
>   ;; characters
>   (string-match (rx (* space) (group (* (not space))) (* space))
> raw i))
>
> Let me know what you think!

Consider cases like

"< \"" "\" >"

or

"< >"

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [FR] :noweb-wrap header arg

2024-05-22 Thread Amy Grinn
Ihor Radchenko  writes:

> Amy Grinn  writes:
>
>>> +1
>>> You may even use obsolete alias (add it to lisp/org-compat.el)
>>
>> Here's a patch to rename org-babel-noweb-wrap to
>> org-babel-noweb-make-regexp.
>
> Thanks!
> News entry is not necessary here - we are just renaming a function.
> Otherwise, the patch looks good.
> I am not yet merging it though - let's have all the patches you plan for
> the new noweb-wrap argument first and then apply them together.

Got it, not a problem!  I've attached both patches here.

Regarding the other conversation,

>>>> +(while (< (point) (point-max))
>>>> +  (unless (looking-at " *\"\\([^\"]+\\)\" *")
>>>> +(looking-at " *\\([^ ]+\\)"))
>>>
>>> May you please explain the rationale behind this regexp? AFAIU, it
>>> implies that you want to allow whitespace characters inside :noweb-wrap
>>> boundaries. But I do not think that we need to complicate things so much.
>>
>> That is exactly what I was going for.  I thought about the ways this
>> could be used and the most general-purpose, non-syntax-breaking,
>> easily-recognizable way I could think of was to use the language's
>> line-comment construct followed by the standard << >> characters.
>>
>> # <>
>> ;; <>
>> // <>
>>
>> I can see how it might be harder to maintain to allow whitespace in the
>> noweb-wrap header arg.  I could create a separate org-parse-arg-list
>> function to ease that burden somewhat.  My opinion is that having the
>> option to use the examples above is preferable to using non-standard
>> wrappers, from a third-person point-of-view.
>
> Makes sense. Also, see a similar idea being discussed in
> https://list.orgmode.org/orgmode/87o7jlzxgn.fsf@localhost/
>
> I recommend the following:
>
> If the value starts from ", use Elisp's `read':
>|"# <<" ">>"
>(read (current-buffer)) ; => "# <<"
>otherwise, consider read until the first whitespace.
>|#<<; >>;
>(re-search-forward (rx (1+ (not whitespace
>#<<;|
>
> However, there may be edge cases like
>
> "<< >>"
> "<< >>
> << << >>
> << "asd" >>

I didn't implement this recommendation yet; I still think the regex is a
more clear way of putting it, even without using a temporary buffer:

;; If a pair of " is found separated by one or more
;; characters, capture those characters as a group
(unless (eq i (string-match (rx (* space) ?\"
    (group (+ (not ?\")))
    ?\" (* space))
    raw i))
  ;; Otherwise, capture the next non-whitespace group of
  ;; characters
  (string-match (rx (* space) (group (* (not space))) (* space))
raw i))

Let me know what you think!
>From 9442c029a7b2f1ec061e42a047b3d1bff88441d8 Mon Sep 17 00:00:00 2001
From: Amy Grinn 
Date: Wed, 17 Apr 2024 16:01:40 -0400
Subject: [PATCH 1/2] lisp/ob-core.el: (org-babel-noweb-wrap): renamed to
 org-babel-noweb-make-regexp

* lisp/org-compat.el: Declare org-babel-noweb-wrap to be an obselete
function alias for org-babel-noweb-make-regexp.
* lisp/ob-core.el (org-babel-noweb-make-regexp): Rename the function.
(org-babel-goto-named-src-block):
(org-babel-expand-noweb-references):
* lisp/ob-exp.el (org-babel-exp-code):
* lisp/ob-tangle.el (org-babel-tangle-clean):
(org-babel-tangle-single-block): Use the new function name.
---
 lisp/ob-core.el| 8 
 lisp/ob-exp.el | 2 +-
 lisp/ob-tangle.el  | 5 +++--
 lisp/org-compat.el | 2 +-
 4 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index c5dd20b0e..1518d7726 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -194,7 +194,7 @@ This string must include a \"%s\" which will be replaced by the results."
   :package-version '(Org . "9.1")
   :safe #'booleanp)
 
-(defun org-babel-noweb-wrap (&optional regexp)
+(defun org-babel-noweb-make-regexp (&optional regexp)
   "Return regexp matching a Noweb reference.
 
 Match any reference, or only those matching REGEXP, if non-nil.
@@ -1976,7 +1976,7 @@ src block, then return nil."
 		   (type (org-element-type context))
 		   (noweb-ref
 		(and (memq type '(inline-src-block src-block))
-			 (org-in-regexp (org-babel-noweb-wrap)
+			 (org-in-regexp (org-babel-noweb-make-regexp)
 	  (cond
 	   (noweb-ref
 		(buffer-substring
@@ -3125,7 +3125,

Re: [FR] :noweb-wrap header arg

2024-05-12 Thread Ihor Radchenko
Amy Grinn  writes:

>> +1
>> You may even use obsolete alias (add it to lisp/org-compat.el)
>
> Here's a patch to rename org-babel-noweb-wrap to
> org-babel-noweb-make-regexp.

Thanks!
News entry is not necessary here - we are just renaming a function.
Otherwise, the patch looks good.
I am not yet merging it though - let's have all the patches you plan for
the new noweb-wrap argument first and then apply them together.

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



Re: [FR] :noweb-wrap header arg

2024-05-11 Thread Amy Grinn
Ihor Radchenko  writes:

> Amy Grinn  writes:
>
>> First of all, I would like to change (defalias) the function name
>> org-babel-noweb-wrap to org-babel-noweb-make-regexp. I think this in
>> more in line with other functions which create regular expressions.
>
> +1
> You may even use obsolete alias (add it to lisp/org-compat.el)

Here's a patch to rename org-babel-noweb-wrap to
org-babel-noweb-make-regexp.

>From b318fef6af8ae47b7e6d0371ccc87a01ed1a7755 Mon Sep 17 00:00:00 2001
From: Amy Grinn 
Date: Wed, 17 Apr 2024 16:01:40 -0400
Subject: [PATCH] lisp/ob-core.el: (org-babel-noweb-wrap): renamed to
 org-babel-noweb-make-regexp

* etc/ORG-NEWS (~org-babel-noweb-wrap~ is now
~org-babel-noweb-make-regexp~): Announce the change.
* lisp/org-compat.el: Declare org-babel-noweb-wrap to be an obselete
function alias for org-babel-noweb-make-regexp.
* lisp/ob-core.el (org-babel-noweb-make-regexp): Rename the function.
(org-babel-goto-named-src-block):
(org-babel-expand-noweb-references):
* lisp/ob-exp.el (org-babel-exp-code):
* lisp/ob-tangle.el (org-babel-tangle-clean):
(org-babel-tangle-single-block): Use the new function name.
---
 etc/ORG-NEWS   | 6 ++
 lisp/ob-core.el| 8 
 lisp/ob-exp.el | 2 +-
 lisp/ob-tangle.el  | 5 +++--
 lisp/org-compat.el | 2 +-
 5 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 978882a7a..97e2f2e3f 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -1563,6 +1563,12 @@ optional argument =NEW-HEADING-CONTAINER= specifies where in the
 buffer it will be added.  If not specified, new headings are created
 at level 1 at the end of the accessible part of the buffer, as before.
 
+** Removed or renamed functions and variables
+*** ~org-babel-noweb-wrap~ is now ~org-babel-noweb-make-regexp~
+
+This is more in line with other functions that return a regular
+expression.
+
 ** Miscellaneous
 *** =org-crypt.el= now applies initial visibility settings to decrypted entries
 
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index c5dd20b0e..1518d7726 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -194,7 +194,7 @@ This string must include a \"%s\" which will be replaced by the results."
   :package-version '(Org . "9.1")
   :safe #'booleanp)
 
-(defun org-babel-noweb-wrap (&optional regexp)
+(defun org-babel-noweb-make-regexp (&optional regexp)
   "Return regexp matching a Noweb reference.
 
 Match any reference, or only those matching REGEXP, if non-nil.
@@ -1976,7 +1976,7 @@ src block, then return nil."
 		   (type (org-element-type context))
 		   (noweb-ref
 		(and (memq type '(inline-src-block src-block))
-			 (org-in-regexp (org-babel-noweb-wrap)
+			 (org-in-regexp (org-babel-noweb-make-regexp)
 	  (cond
 	   (noweb-ref
 		(buffer-substring
@@ -3125,7 +3125,7 @@ block but are passed literally to the \"example-block\"."
   (not (equal (cdr v) "no"))
 	 (noweb-re (format "\\(.*?\\)\\(%s\\)"
 			   (with-current-buffer parent-buffer
-			 (org-babel-noweb-wrap)
+			 (org-babel-noweb-make-regexp)
 (unless (equal (cons parent-buffer
  (with-current-buffer parent-buffer
(buffer-chars-modified-tick)))
@@ -3175,7 +3175,7 @@ block but are passed literally to the \"example-block\"."
 	   ((guard (or org-babel-noweb-error-all-langs
 			   (member lang org-babel-noweb-error-langs)))
 	    (error "Cannot resolve %s (see `org-babel-noweb-error-langs')"
-		   (org-babel-noweb-wrap ,ref)))
+		   (org-babel-noweb-make-regexp ,ref)))
 	   (_ ""
   (replace-regexp-in-string
noweb-re
diff --git a/lisp/ob-exp.el b/lisp/ob-exp.el
index 80eaeeb27..a61b26ed5 100644
--- a/lisp/ob-exp.el
+++ b/lisp/ob-exp.el
@@ -419,7 +419,7 @@ replaced with its value."
   (setf (nth 1 info)
 	(if (string= "strip-export" (cdr (assq :noweb (nth 2 info
 	(replace-regexp-in-string
-	 (org-babel-noweb-wrap) "" (nth 1 info))
+	 (org-babel-noweb-make-regexp) "" (nth 1 info))
 	  (if (org-babel-noweb-p (nth 2 info) :export)
 	  (org-babel-expand-noweb-references
 	   info org-babel-exp-reference-buffer)
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 79fe6448b..4427250ae 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -412,7 +412,7 @@ references."
   (interactive)
   (goto-char (point-min))
   (while (or (re-search-forward "\\[\\[file:.*\\]\\[.*\\]\\]" nil t)
- (re-search-forward (org-babel-noweb-wrap) nil t))
+ (re-search-forward (org-babel-noweb-make-regexp) nil t))
 (delete-region (save-excursion (forward-line) (point))
    (save-ex

Re: [Q/Bug?] Comment for noweb reference comments content

2024-05-04 Thread Ihor Radchenko
João Pedro  writes:

> #+begin_src emacs-lisp :noweb yes :comments noweb :tangle /tmp/foo.el
> (progn
>   <>)
> #+end_src
>
> #+name: foo
> #+begin_src emacs-lisp
> (message "foo")
> #+end_src
> ...
> ;; [[file:...][No heading:1]]
> (progn
>   ;; [[file:...][foo]]
>   (message "foo")
>   ;; foo ends here)

Thanks for reporting!
Fixed, on main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=1523e21d8

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



[Q/Bug?] Comment for noweb reference comments content

2024-05-03 Thread João Pedro
Hi,

Ok so I don't know whether this is a bug or intended behaviour (though
if its the latter I propose we reconsider), but if I have the following

#+begin_src emacs-lisp :noweb yes :comments noweb :tangle /tmp/foo.el
(progn
  <>)
#+end_src

#+name: foo
#+begin_src emacs-lisp
(message "foo")
#+end_src

the resulting foo.el will have a syntax error, since the closing ) for
`progn' will have been commented like so

;; [[file:...][No heading:1]]
(progn
  ;; [[file:...][foo]]
  (message "foo")
  ;; foo ends here)
  ^
;; No heading:1 ends here

It seems like some macro let-bound in `org-babel-expand-noweb-refereces'
is doing this, but I couldn't pinpoint exactly what or where. What I
could achieve though, is to add a newline after the second call to
`c-wrap' inside `expand-body''s definition, which at least solve the
problem of throwing syntax errors, but I don't think that would be the
best solution.

Best regards,

-- 
João Pedro de A. Paula
IT bachelors at Universidade Federal do Rio Grande do Norte (UFRN)


Re: [FR] :noweb-wrap header arg

2024-04-13 Thread Ihor Radchenko
Amy Grinn  writes:

>>> +(while (< (point) (point-max))
>>> +  (unless (looking-at " *\"\\([^\"]+\\)\" *")
>>> +(looking-at " *\\([^ ]+\\)"))
>>
>> May you please explain the rationale behind this regexp? AFAIU, it
>> implies that you want to allow whitespace characters inside :noweb-wrap
>> boundaries. But I do not think that we need to complicate things so much.
>
> That is exactly what I was going for.  I thought about the ways this
> could be used and the most general-purpose, non-syntax-breaking,
> easily-recognizable way I could think of was to use the language's
> line-comment construct followed by the standard << >> characters.
>
> # <>
> ;; <>
> // <>
>
> I can see how it might be harder to maintain to allow whitespace in the
> noweb-wrap header arg.  I could create a separate org-parse-arg-list
> function to ease that burden somewhat.  My opinion is that having the
> option to use the examples above is preferable to using non-standard
> wrappers, from a third-person point-of-view.

Makes sense. Also, see a similar idea being discussed in
https://list.orgmode.org/orgmode/87o7jlzxgn.fsf@localhost/

I recommend the following:

If the value starts from ", use Elisp's `read':
   |"# <<" ">>"
   (read (current-buffer)) ; => "# <<"
   otherwise, consider read until the first whitespace.
   |#<<; >>;
   (re-search-forward (rx (1+ (not whitespace
   #<<;|
   
However, there may be edge cases like

"<< >>"
"<< >>
<< << >>
<< "asd" >>

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



Re: [FR] :noweb-wrap header arg

2024-04-11 Thread Amy Grinn
Ihor Radchenko  writes:

> Amy Grinn  writes:
>
>> +(insert raw)
>> +(goto-char (point-min))
>> +(while (< (point) (point-max))
>> +  (unless (looking-at " *\"\\([^\"]+\\)\" *")
>> +(looking-at " *\\([^ ]+\\)"))
>
> May you please explain the rationale behind this regexp? AFAIU, it
> implies that you want to allow whitespace characters inside :noweb-wrap
> boundaries. But I do not think that we need to complicate things so much.

That is exactly what I was going for.  I thought about the ways this
could be used and the most general-purpose, non-syntax-breaking,
easily-recognizable way I could think of was to use the language's
line-comment construct followed by the standard << >> characters.

# <>
;; <>
// <>

I can see how it might be harder to maintain to allow whitespace in the
noweb-wrap header arg.  I could create a separate org-parse-arg-list
function to ease that burden somewhat.  My opinion is that having the
option to use the examples above is preferable to using non-standard
wrappers, from a third-person point-of-view.



Re: [FR] :noweb-wrap header arg

2024-04-11 Thread Ihor Radchenko
Amy Grinn  writes:

> First of all, I would like to change (defalias) the function name
> org-babel-noweb-wrap to org-babel-noweb-make-regexp. I think this in
> more in line with other functions which create regular expressions.

+1
You may even use obsolete alias (add it to lisp/org-compat.el)

> Second, the command org-babel-tangle-clean is not able to determine
> which noweb syntax is being used in any tangled source file because the
> header arguments are not tangled along with the source code.
>
> My proposal is to add an additional warning to this command, stating:
>
> """
> Warning, this command removes any lines containing constructs which
> resemble Org file links or noweb references.  It also cannot determine
> which noweb syntax is being used for any given source file, if
> :noweb-wrap was specified in the original Org file.
> """

Makes sense.
Ideally, this function should try to follow the link and lookup code
block headers, but it is already short of doing this. Improving
`org-babel-tangle-clean' is out of scope of your patch.

> +(defun org-babel-get-noweb-wrap (&optional info)
> +  "Retrieve a description the :noweb-wrap header arg from INFO.
> +
> +The description will be in the form of a list of two of strings
> +for the start and end of a reference.  INFO can be the result of
> +`org-babel-get-src-block-info' otherwise this function will parse
> +info at point."
> +  (unless info
> +(setq info (org-babel-get-src-block-info 'no-eval)))
> +  (when-let ((raw (cdr (assq :noweb-wrap (nth 2 info)
> +(let (result)
> +  (with-temp-buffer

Please, avoid creating throwaway buffers and work with strings instead.
Creating buffers (even temporary buffers) may be costly for some users
who heavily customized buffer hooks.

> +(insert raw)
> +(goto-char (point-min))
> +(while (< (point) (point-max))
> +  (unless (looking-at " *\"\\([^\"]+\\)\" *")
> +(looking-at " *\\([^ ]+\\)"))

May you please explain the rationale behind this regexp? AFAIU, it
implies that you want to allow whitespace characters inside :noweb-wrap
boundaries. But I do not think that we need to complicate things so much.

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



[FR] :noweb-wrap header arg

2024-04-08 Thread Amy Grinn

Hi!

I'm working on the :noweb-wrap header argument which controls the syntax
of noweb references in a babel src block. For example:

#+name: foo
#+begin_src elisp
  :foo
#+end_src

#+begin_src elisp :noweb yes :noweb-wrap <<< >>>
  <<>>
#+end_src

And I would like some feedback...

First of all, I would like to change (defalias) the function name
org-babel-noweb-wrap to org-babel-noweb-make-regexp. I think this in
more in line with other functions which create regular expressions.

The new way to retrieve the regular expression matching a noweb
reference in the source block at point would be:

(org-babel-noweb-make-regexp nil (org-babel-get-noweb-wrap info))

Where info can be nil or the result of calling
(org-babel-get-src-block-info).

Second, the command org-babel-tangle-clean is not able to determine
which noweb syntax is being used in any tangled source file because the
header arguments are not tangled along with the source code.

My proposal is to add an additional warning to this command, stating:

"""
Warning, this command removes any lines containing constructs which
resemble Org file links or noweb references.  It also cannot determine
which noweb syntax is being used for any given source file, if
:noweb-wrap was specified in the original Org file.
"""

Best,

Amy

>From 1dc8aebcc45447d3b5b38ea3c7700ae2b2686c9d Mon Sep 17 00:00:00 2001
From: Amy Grinn 
Date: Mon, 8 Apr 2024 09:05:02 -0400
Subject: [PATCH] (WIP) lisp/ob-core.el: New :noweb-wrap header arg

* lisp/ob-core: (org-babel-noweb-wrap): Add optional third parameter
'wrap'.
* lisp/ob-core: (org-babel-get-noweb-wrap): New function for parsing
:noweb-wrap header arg.
* etc/ORG-NEWS (New =:noweb-wrap= babel header argument): Describe new
argument.
* others...
---
 etc/ORG-NEWS| 14 ++
 lisp/ob-core.el | 51 --
 lisp/ob-exp.el  |  3 +-
 lisp/ob-tangle.el   |  6 +++-
 testing/examples/babel.org  | 17 
 testing/lisp/test-ob-exp.el | 55 +
 testing/lisp/test-ob.el | 15 ++
 7 files changed, 150 insertions(+), 11 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index aeb7ffd4b..162e7f035 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -621,6 +621,20 @@ link when storing any type of external link type in an Org file, not
 just =id:= links.
 
 ** New and changed options
+*** New =:noweb-wrap= babel header argument
+
+This argument changes the default noweb reference syntax by masking
+the options ~org-babel-noweb-wrap-start~ and
+~org-babel-noweb-wrap-end~.
+
+=:noweb-wrap= takes two parameters, start and end, corresponding to
+each option.
+
+For example:
+: #+begin_src sh :noweb-wrap <<< >>>
+:   echo <<>>
+: #+end_src
+
 *** =.avif= images are now recognized in ~org-html-inline-image-rules~
 
 In =ox-html=, =.avif= image links are now inlined by default.
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 8dfc07a4e..843794322 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -194,15 +194,21 @@ This string must include a \"%s\" which will be replaced by the results."
   :package-version '(Org . "9.1")
   :safe #'booleanp)
 
-(defun org-babel-noweb-wrap (&optional regexp)
+(defun org-babel-noweb-wrap (&optional regexp wrap)
   "Return regexp matching a Noweb reference.
 
 Match any reference, or only those matching REGEXP, if non-nil.
 
+If WRAP is provided, it should be a list of 2 strings describing
+the start and end of a noweb reference, such as that returned by
+`org-babel-get-noweb-wrap'.  Otherwise
+`org-babel-noweb-wrap-start' and `org-babel-noweb-wrap-end' will
+be used.
+
 When matching, reference is stored in match group 1."
-  (concat (regexp-quote org-babel-noweb-wrap-start)
-	  (or regexp "\\([^ \t\n]\\(?:.*?[^ \t\n]\\)?\\)")
-	  (regexp-quote org-babel-noweb-wrap-end)))
+  (concat (regexp-quote (or (car wrap) org-babel-noweb-wrap-start))
+	(or regexp "\\([^ \t\n]\\(?:.*?[^ \t\n]\\)?\\)")
+	(regexp-quote (or (cadr wrap) org-babel-noweb-wrap-end
 
 (defvar org-babel-src-name-regexp
   "^[ \t]*#\\+name:[ \t]*"
@@ -1963,6 +1969,27 @@ src block, then return nil."
   (let ((head (org-babel-where-is-src-block-head)))
 (if head (goto-char head) (error "Not currently in a code block"
 
+(defun org-babel-get-noweb-wrap (&optional info)
+  "Retrieve a description the :noweb-wrap header arg from INFO.
+
+The description will be in the form of a list of two of strings
+for the start and end of a reference.  INFO can be the result of
+`org-babel-get-src-block-info' otherwise this function will parse
+info at point."
+  (unless info
+(setq info (org-babel-get-src-block-info 'no-eval)))
+  (when-let ((raw (cdr (assq :n

Re: Support for whitespace prefix for :noweb-prefix

2024-04-03 Thread Fraga, Eric
Ah, okay.  Thank you.
-- 
: Eric S Fraga, with org release_9.6.23-1314-g945046 in Emacs 30.0.50


Re: Support for whitespace prefix for :noweb-prefix

2024-04-03 Thread Ihor Radchenko
"Fraga, Eric"  writes:

> So it's a combination of no prefix in such a case and the indentation
> according to major mode.  For me, the latter is not an issue but the
> former is.
>
> (but not really an issue for me as I make sure I only have noweb
> references on lines where such a replacement will not cause any
> problems...)

You can just use :noweb-prefix no. It is already a part of Org.
What we are discussing is adding more functionality to this new
parameter.

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



Re: Support for whitespace prefix for :noweb-prefix

2024-04-03 Thread Fraga, Eric
On Friday, 29 Mar 2024 at 09:43, Ihor Radchenko wrote:
> Doerthous  writes:
>> #+begin_src elisp
>> (let ((a 0)
>> (let ((b 1))
>>`(,a ,b))
>> #+end_src
>>
>> ~(let (~ is the prefix of <>.
>>
>> I thought we can replace just the prefix in current code[1]  with
>> ~(setq prefix (replace-regexp-in-string "[^ \t]" " " prefix))~ ?
>
> I see.
> So, you want a custom prefix before the expanded noweb reference lines.

Not only that; at the moment, the prefix is defined by the text that
comes before the noweb reference and hence why the "(let (" is repeated
on the second line in the code block above.

So it's a combination of no prefix in such a case and the indentation
according to major mode.  For me, the latter is not an issue but the
former is.

(but not really an issue for me as I make sure I only have noweb
references on lines where such a replacement will not cause any
problems...)

-- 
: Eric S Fraga, with org release_9.6.23-1314-g945046 in Emacs 30.0.50


Re: Support for whitespace prefix for :noweb-prefix

2024-03-29 Thread Doerthous
Thanks, I'll check it out.



Re: Support for whitespace prefix for :noweb-prefix

2024-03-29 Thread Ihor Radchenko
Doerthous  writes:

> I kind of understand what you mean: what I need is just the no
> option, after the expansion (maybe with the help of some
> after-expansion-hooks), indent the source block according to
> major-mode. Right?
>
> I will try to find some hooks after expansion and some APIs to
> indent a source block?
>
> Are the any tips for doing this? :-)

Maybe `org-babel-tangle-body-hook'. If your problem is how the tangled
code looks like.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: Support for whitespace prefix for :noweb-prefix

2024-03-29 Thread Doerthous
Ihor Radchenko  writes:

> I still feel that what you are really looking for is major-mode-specific
> indentation, not a prefix. Because indentation may require tabs, not
> spaces. Or may interfere with programming language.

I kind of understand what you mean: what I need is just the no
option, after the expansion (maybe with the help of some
after-expansion-hooks), indent the source block according to
major-mode. Right?

I will try to find some hooks after expansion and some APIs to
indent a source block?

Are the any tips for doing this? :-)

Thanks in advance.



Re: Support for whitespace prefix for :noweb-prefix

2024-03-29 Thread Ihor Radchenko
Doerthous  writes:

> Ihor Radchenko  writes:
>
>> I think that a more general approach could be allowing :noweb-prefix to
>> have a value of string that will be appended to each line on the
>> expansion.
>
> With the use case discussed before, is allowing :noweb-prefix to have a
> value of string means I need to provide a string with 5 space
> characters to :noweb-ref like this?

> #+begin_src elisp :noweb yes :noweb-prefix " "
>   (let (<>)
> <>)
> #+end_src

Yes.

I still feel that what you are really looking for is major-mode-specific
indentation, not a prefix. Because indentation may require tabs, not
spaces. Or may interfere with programming language.

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



Re: Support for whitespace prefix for :noweb-prefix

2024-03-29 Thread Doerthous
Ihor Radchenko  writes:

> I think that a more general approach could be allowing :noweb-prefix to
> have a value of string that will be appended to each line on the
> expansion.

With the use case discussed before, is allowing :noweb-prefix to have a
value of string means I need to provide a string with 5 space
characters to :noweb-ref like this?

#+begin_src elisp :noweb yes :noweb-prefix " "
  (let (<>)
<>)
#+end_src

Or is it some other way?



Re: Support for whitespace prefix for :noweb-prefix

2024-03-29 Thread Ihor Radchenko
Doerthous  writes:

>> Do you mean that you want the code to be indented according to the major
>> mode rules?
>>
>
> Why it relates to major mode,
>
> Currently, with :noweb-prefix set to yes, the above code will be expand to
> #+begin_src elisp
> (let ((a 0)
> (let ((b 1))
>`(,a ,b))
> #+end_src
>
> ~(let (~ is the prefix of <>.
>
> I thought we can replace just the prefix in current code[1]  with
> ~(setq prefix (replace-regexp-in-string "[^ \t]" " " prefix))~ ?

I see.
So, you want a custom prefix before the expanded noweb reference lines.

I think that a more general approach could be allowing :noweb-prefix to
have a value of string that will be appended to each line on the
expansion.

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



Re: Support for whitespace prefix for :noweb-prefix

2024-03-28 Thread Doerthous
Ihor Radchenko  于2024年3月29日周五 03:25写道:
>
> Doerthous  writes:
>
> > Can we add a support for whitespace prefix such that the prefix of a
> > noweb-ref replaced by whitespace characters?
> >
> > ...
> > #+name: a-fragment
> > #+begin_src elisp :noweb yes :noweb-prefix whitespace
> >   (let (<>)
> > <>)
> > #+end_src
> >
> > using whitespace :noweb-prefix, the above code will expand to
> >
> > #+begin_src elisp
> > (let ((a 0)
> >   (b 1))
> >   `(,a ,b))
> > #+end_src
>
> Do you mean that you want the code to be indented according to the major
> mode rules?
>

Why it relates to major mode,

Currently, with :noweb-prefix set to yes, the above code will be expand to
#+begin_src elisp
(let ((a 0)
(let ((b 1))
   `(,a ,b))
#+end_src

~(let (~ is the prefix of <>.

I thought we can replace just the prefix in current code[1]  with
~(setq prefix (replace-regexp-in-string "[^ \t]" " " prefix))~ ?

[1] lisp/ob-core.el (org-babel-expand-noweb-references) @ 9.6.23



Re: Support for whitespace prefix for :noweb-prefix

2024-03-28 Thread Ihor Radchenko
Doerthous  writes:

> Can we add a support for whitespace prefix such that the prefix of a
> noweb-ref replaced by whitespace characters?
>
> ...
> #+name: a-fragment
> #+begin_src elisp :noweb yes :noweb-prefix whitespace
>   (let (<>)
> <>)
> #+end_src
>
> using whitespace :noweb-prefix, the above code will expand to
>
> #+begin_src elisp
> (let ((a 0)
>   (b 1))
>   `(,a ,b))
> #+end_src

Do you mean that you want the code to be indented according to the major
mode rules?

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



[FR] Add whitespace option for :noweb-prefix

2024-03-28 Thread Chen Mingzheng


Hi


For :noweb-prefix of noweb feature, now we have "yes" or "no" option.
Can we add a new "whitespace" option so that we can:

Giving following fragments,
  #+begin_src elisp :noweb-ref varable-bindings
  (a 0)
  (b 1)
  #+end_src

  #+begin_src elisp :noweb-ref do-something
  `(,a ,b)
  #+end_src

we use the "whitespace" option here
  #+name: a-fragment
  #+begin_src elisp :noweb yes :noweb-prefix whitespace
(let (<>)
  <>)
  #+end_src

to produce the follow code:
  #+begin_src elisp
  (let ((a 0)
(b 1))
`(,a ,b))
  #+end_src


Here is a try:
  #+begin_src elisp :exports results :wrap src elisp
  (org-babel--expand-body (org-babel-lob--src-info "a-fragment"))
  #+end_src
BTW, I'm curious if there's an public API that can output the code expansion of 
an noweb src block by it's name like above?


Best regards,

Chen



Add new whitespace option for :noweb-prefix

2024-03-28 Thread Chen Mingzheng


Hi


For :noweb-prefix of noweb feature, now we have "yes" or "no" option.
Can we add a new "whitespace" option so that we can:

Giving following fragments,
  #+begin_src elisp :noweb-ref varable-bindings
  (a 0)
  (b 1)
  #+end_src

  #+begin_src elisp :noweb-ref do-something
  `(,a ,b)
  #+end_src

we use the "whitespace" option here
  #+name: a-fragment
  #+begin_src elisp :noweb yes :noweb-prefix whitespace
(let (<>)
  <>)
  #+end_src

to produce the follow code:
  #+begin_src elisp
  (let ((a 0)
(b 1))
`(,a ,b))
  #+end_src


Here is a try:
  #+begin_src elisp :exports results :wrap src elisp
  (org-babel--expand-body (org-babel-lob--src-info "a-fragment"))
  #+end_src
BTW, I'm curious if there's an public API that can output the code expansion of 
an noweb src block by it's name like above?


Best regards,

Chen



Support for whitespace prefix for :noweb-prefix

2024-03-28 Thread Doerthous
Hi

Can we add a support for whitespace prefix such that the prefix of a
noweb-ref replaced by whitespace characters?

Here is a use case,

#+begin_src elisp :noweb-ref varable-bindings
(a 0)
(b 1)
#+end_src

#+begin_src elisp :noweb-ref do-something
`(,a ,b)
#+end_src

#+name: a-fragment
#+begin_src elisp :noweb yes :noweb-prefix whitespace
  (let (<>)
<>)
#+end_src

using whitespace :noweb-prefix, the above code will expand to

#+begin_src elisp
(let ((a 0)
  (b 1))
  `(,a ,b))
#+end_src

Best regards,

D



Re: noweb-start and noweb-end header args

2024-03-07 Thread Amy Grinn
Ihor Radchenko  writes:

> Amy Grinn  writes:
>
>> Here is a simple way to implement this feature.
>
> Since you are adding a new feature, it should have (1) test coverage;
> (2) be documented in the manual; (3) be announced in etc/ORG-NEWS.

Thank you for the tips, will do!



Re: noweb-start and noweb-end header args

2024-03-07 Thread Ihor Radchenko
Amy Grinn  writes:

> I kinda disagree with your reasoning but I agree with your conclusion.
> I think it would be strange for third party or user configuration to
> parse the value of a noweb header argument directly.  Org provides a
> lublic api for this which should be backwards compatible:
>
>   (org-babel-noweb-p (nth 2 (org-babel-get-src-block-info t)) :tangle)

Yeah, but this API is not uniform. Most other header arguments must be
retrieved using `assq'. So, it is very common to use `assq' for
everything, even if there is a specific API.
(Ideally, we need to implement a uniform API to retrieve header argument
values)

> The middle way is to create just one new header arg:
>
> :noweb-wrap  [end]

Maybe  should be mandatory. Otherwise, LGTM.


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



Re: noweb-start and noweb-end header args

2024-03-07 Thread Amy Grinn
Ihor Radchenko  writes:

> :noweb yes <<< >>> is actually backwards-incompatible. Consider
> third-party code that makes assumptions about possible values of
> :noweb
> header argument. If third-party code does a check like
> (equal noweb-value "yes"), the new syntax can break such code.

I kinda disagree with your reasoning but I agree with your conclusion.
I think it would be strange for third party or user configuration to
parse the value of a noweb header argument directly.  Org provides a
lublic api for this which should be backwards compatible:

  (org-babel-noweb-p (nth 2 (org-babel-get-src-block-info t)) :tangle)

However, other parsers besides Emacs exist for Org mode and they may
directly compare the entire noweb argument.

The middle way is to create just one new header arg:

:noweb-wrap  [end]



Re: noweb-start and noweb-end header args

2024-03-07 Thread Ihor Radchenko
Amy Grinn  writes:

> Here is a simple way to implement this feature.
> ...
> -(defun org-babel-noweb-wrap (&optional regexp)
> +(defun org-babel-noweb-wrap (&optional regexp info)
>"Return regexp matching a Noweb reference.
>  
>  Match any reference, or only those matching REGEXP, if non-nil.
>  
>  When matching, reference is stored in match group 1."

INFO argument needs to be documented.

> -  (org-babel-noweb-wrap)))))
> +  (org-babel-noweb-wrap nil info)

Please, also update all other cases when `org-babel-noweb-wrap' is
called.

Also, `org-babel-goto-named-src-block' uses
org-babel-noweb-wrap-start/end directly. It should be adjusted.

Since you are adding a new feature, it should have (1) test coverage;
(2) be documented in the manual; (3) be announced in etc/ORG-NEWS.

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



Re: noweb-start and noweb-end header args

2024-03-07 Thread Ihor Radchenko
Amy Grinn  writes:

> To expand on this, some major modes can fundamentally conflict with the
> default noweb syntax.  Here is a valid shell script *and* a valid noweb
> reference to a block named EOF:
>
> cat <> file.txt
> Hello
> EOF
>
> I hope this helps explain why the wrap-start and wrap-end options were
> necessary to include more than a decade ago.  In terms of actually using
> them, it's a bit cumbersome, especially in Org mode buffers that use
> multiple languages.

This makes sense.
I agree that setting noweb reference syntax per-language is useful in
some cases.

> The second diff I sent (under the termux handle, accidentally) is my
> preferred solution (:noweb yes <<< >>>).  This would avoid the need for
> new header arguments to be introduced while maintaining backwards
> compatibility.  It also feels natural to specify the two options
> together: I can't think of a good reason to only need to specify the
> wrap-end option.

:noweb yes <<< >>> is actually backwards-incompatible. Consider
third-party code that makes assumptions about possible values of :noweb
header argument. If third-party code does a check like
(equal noweb-value "yes"), the new syntax can break such code.

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



Re: noweb-start and noweb-end header args

2024-03-07 Thread Ihor Radchenko
Amy Grinn  writes:

>> Org mode does not _currently_ modify the code. But that's actually wrong
>> - things like escaped ,* or indentation sometimes also stay on the way
>> and produce incorrect fontification. So, rewriting the fontification of
>> src blocks to cleanup the code before fontification is long due.
>> noweb references is just another manifestation of this problem.
>
> I think we're talking past each other a little.  I'm not talking about
> changing the text content of a src block, I'm talking about modifying
> the syntax table of a major mode such as sh-mode to ignore or handle
> <> syntax in an "edit-special" buffer.  That was my
> interpretation of your suggestion of using fontification to solve this
> issue.  And if that's the case, I foresee a lot of edge cases for
> modifying the display of major modes.

That's not what I had in mind. I thought of resolving/replacing noweb
references before fontifying the code. That way, the major mode for src
block will simply not see <> text and will not be confused.

>> I am not in favor of adding features that aim to serve as workarounds to
>> Org mode.
>
> This discussion is not about whether to allow users to modify noweb
> syntax.  That feature is already a part of Org, well documented, and
> utilized.  The feature request I'm making is to allow that modification
> to be done on a per-block level.

Sure, but I wanted to hear why such feature is useful in practice. Your
example with fontification is not something I consider as a good
justification for adding a new feature. You another email provides a
better justification though.

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



Re: noweb-start and noweb-end header args

2024-03-06 Thread Amy Grinn
Amy Grinn  writes:

> Ihor Radchenko  writes:
>
>> Amy Grinn  writes:
>>
>>> I would like to add support for setting 'org-babel-noweb-wrap-start and
>>> 'org-babel-noweb-wrap-end for each src block individually
>>
>> May you please explain the use case when changing the default values
>> is useful?
>
> Of course!  Changing the default values can be useful to prevent syntax
> highlighting errors in a specific language.  In the example I gave, <<<
> and >>> aren't recognized as the beginning of a heredoc in a shell
> script the way <> would be.

To expand on this, some major modes can fundamentally conflict with the
default noweb syntax.  Here is a valid shell script *and* a valid noweb
reference to a block named EOF:

cat <> file.txt
Hello
EOF

I hope this helps explain why the wrap-start and wrap-end options were
necessary to include more than a decade ago.  In terms of actually using
them, it's a bit cumbersome, especially in Org mode buffers that use
multiple languages.

The second diff I sent (under the termux handle, accidentally) is my
preferred solution (:noweb yes <<< >>>).  This would avoid the need for
new header arguments to be introduced while maintaining backwards
compatibility.  It also feels natural to specify the two options
together: I can't think of a good reason to only need to specify the
wrap-end option.



Re: noweb-start and noweb-end header args

2024-03-06 Thread Amy Grinn
Ihor Radchenko  writes:

> Amy Grinn  writes:
>
>> How much does org mode modify the fontification for an indirect buffer?
>> Without having looked into it, I assume not much or at all.
>> ... I think
>> that approach could be more complex, especially when dealing with a
>> theoretically infinite number of major modes.
>
> Org mode does not _currently_ modify the code. But that's actually wrong
> - things like escaped ,* or indentation sometimes also stay on the way
> and produce incorrect fontification. So, rewriting the fontification of
> src blocks to cleanup the code before fontification is long due.
> noweb references is just another manifestation of this problem.

I think we're talking past each other a little.  I'm not talking about
changing the text content of a src block, I'm talking about modifying
the syntax table of a major mode such as sh-mode to ignore or handle
<> syntax in an "edit-special" buffer.  That was my
interpretation of your suggestion of using fontification to solve this
issue.  And if that's the case, I foresee a lot of edge cases for
modifying the display of major modes.

>> Both solutions could be implemented at the same time.  We could build on
>> the existing functionality of the wrap-end and wrap-start variables
>> while also looking at ways to modify the syntax highlighting without
>> user intervention.
>
> I am not in favor of adding features that aim to serve as workarounds to
> Org mode.

This discussion is not about whether to allow users to modify noweb
syntax.  That feature is already a part of Org, well documented, and
utilized.  The feature request I'm making is to allow that modification
to be done on a per-block level.



Re: noweb-start and noweb-end header args

2024-03-06 Thread Ihor Radchenko
Amy Grinn  writes:

>> This sounds like XY problem then.
>> If the real problem you want to solve is fontification, we may instead
>> adjust Org mode fontification of source blocks to exclude noweb
>> references.
>
> I see a problem with multiple possible solutions, some more involved
> than others.  The org-babel-noweb-wrap-* variables are already
> customizeable and, in researching a solution to this problem, I have
> found users who set these variables on a file or directory-local level
> already.

Yup. And most of these customizations are aiming to solve the
fontification problem. If we did not have the problem to start with,
re-defining the noweb wrap syntax would be unnecessary in many cases.

> How much does org mode modify the fontification for an indirect buffer?
> Without having looked into it, I assume not much or at all.
> ... I think
> that approach could be more complex, especially when dealing with a
> theoretically infinite number of major modes.

Org mode does not _currently_ modify the code. But that's actually wrong
- things like escaped ,* or indentation sometimes also stay on the way
and produce incorrect fontification. So, rewriting the fontification of
src blocks to cleanup the code before fontification is long due.
noweb references is just another manifestation of this problem.

> Both solutions could be implemented at the same time.  We could build on
> the existing functionality of the wrap-end and wrap-start variables
> while also looking at ways to modify the syntax highlighting without
> user intervention.

I am not in favor of adding features that aim to serve as workarounds to
Org mode.

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



Re: noweb-start and noweb-end header args

2024-03-06 Thread Amy Grinn
Ihor Radchenko  writes:

> Amy Grinn  writes:
>
>>>> #+name: firewall
>>>> #+begin_src sh  :noweb yes :noweb-start <<< :noweb-end >>>
>>>
>>> May you please explain the use case when changing the default values
>>> is useful?
>>
>> Of course!  Changing the default values can be useful to prevent syntax
>> highlighting errors in a specific language.  In the example I gave, <<<
>> and >>> aren't recognized as the beginning of a heredoc in a shell
>> script the way <> would be.
>
> This sounds like XY problem then.
> If the real problem you want to solve is fontification, we may instead
> adjust Org mode fontification of source blocks to exclude noweb
> references.

I see a problem with multiple possible solutions, some more involved
than others.  The org-babel-noweb-wrap-* variables are already
customizeable and, in researching a solution to this problem, I have
found users who set these variables on a file or directory-local level
already.

How much does org mode modify the fontification for an indirect buffer?
Without having looked into it, I assume not much or at all.  I think
that approach could be more complex, especially when dealing with a
theoretically infinite number of major modes.

Both solutions could be implemented at the same time.  We could build on
the existing functionality of the wrap-end and wrap-start variables
while also looking at ways to modify the syntax highlighting without
user intervention.



Re: noweb-start and noweb-end header args

2024-03-06 Thread Ihor Radchenko
Amy Grinn  writes:

>>> #+name: firewall
>>> #+begin_src sh  :noweb yes :noweb-start <<< :noweb-end >>>
>>
>> May you please explain the use case when changing the default values
>> is useful?
>
> Of course!  Changing the default values can be useful to prevent syntax
> highlighting errors in a specific language.  In the example I gave, <<<
> and >>> aren't recognized as the beginning of a heredoc in a shell
> script the way <> would be.

This sounds like XY problem then.
If the real problem you want to solve is fontification, we may instead
adjust Org mode fontification of source blocks to exclude noweb
references.

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



Re: noweb-start and noweb-end header args

2024-03-06 Thread Amy Grinn
Ihor Radchenko  writes:

> Amy Grinn  writes:
>
>> I would like to add support for setting 'org-babel-noweb-wrap-start and
>> 'org-babel-noweb-wrap-end for each src block individually using the
>> header args :noweb-start and :noweb-end:
>> ...
>> #+name: firewall
>> #+begin_src sh  :noweb yes :noweb-start <<< :noweb-end >>>
>
> May you please explain the use case when changing the default values
> is useful?

Of course!  Changing the default values can be useful to prevent syntax
highlighting errors in a specific language.  In the example I gave, <<<
and >>> aren't recognized as the beginning of a heredoc in a shell
script the way <> would be.



Re: noweb-start and noweb-end header args

2024-03-06 Thread Ihor Radchenko
Amy Grinn  writes:

> I would like to add support for setting 'org-babel-noweb-wrap-start and
> 'org-babel-noweb-wrap-end for each src block individually using the
> header args :noweb-start and :noweb-end:
> ...
> #+name: firewall
> #+begin_src sh  :noweb yes :noweb-start <<< :noweb-end >>>

May you please explain the use case when changing the default values is useful?

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



Re: noweb-start and noweb-end header args

2024-03-05 Thread termux
Amy Grinn  writes:

> I would like to add support for setting 'org-babel-noweb-wrap-start and
> 'org-babel-noweb-wrap-end for each src block individually using the
> header args :noweb-start and :noweb-end:

Here's another possible syntax we could use:

:noweb  [wrap-start] [wrap-end]

#+name: message
#+begin_src elisp
  "Firewall is now in safe mode."
#+end_src

#+name: firewall-safe-mode
#+begin_src sh :noweb yes
  echo <>
#+end_src

#+name: firewall
#+begin_src sh :noweb yes <<< >>>
  safe_mode () {
  echo "Error encountered, switching to safe mode."
  <<>>
  exit 1
  }
#+end_src

#+begin_src sh :noweb yes ":-) " " (-:" :tangle "test.sh"
  # Setup firewall
  :-) firewall (-:

  # Do other things
#+end_src

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 4dcfbd3b0..f60b4be12 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -194,15 +194,20 @@ This string must include a \"%s\" which will be replaced by the results."
   :package-version '(Org . "9.1")
   :safe #'booleanp)
 
-(defun org-babel-noweb-wrap (&optional regexp)
+(defun org-babel-noweb-wrap (&optional regexp info)
   "Return regexp matching a Noweb reference.
 
 Match any reference, or only those matching REGEXP, if non-nil.
 
 When matching, reference is stored in match group 1."
-  (concat (regexp-quote org-babel-noweb-wrap-start)
-	  (or regexp "\\([^ \t\n]\\(?:.*?[^ \t\n]\\)?\\)")
-	  (regexp-quote org-babel-noweb-wrap-end)))
+  (let ((noweb (mapcar
+    (lambda (token)
+  (if (stringp token) token (symbol-name token)))
+(org-babel-read
+ (format "'(%s)" (cdr (assq :noweb (nth 2 info))))
+(concat (regexp-quote (or (nth 1 noweb) org-babel-noweb-wrap-start))
+	(or regexp "\\([^ \t\n]\\(?:.*?[^ \t\n]\\)?\\)")
+	(regexp-quote (or (nth 2 noweb) org-babel-noweb-wrap-end)
 
 (defvar org-babel-src-name-regexp
   "^[ \t]*#\\+name:[ \t]*"
@@ -3116,7 +3121,7 @@ block but are passed literally to the \"example-block\"."
   (not (equal (cdr v) "no"))
 	 (noweb-re (format "\\(.*?\\)\\(%s\\)"
 			   (with-current-buffer parent-buffer
-			 (org-babel-noweb-wrap)
+			 (org-babel-noweb-wrap nil info)
 (unless (equal (cons parent-buffer
  (with-current-buffer parent-buffer
(buffer-chars-modified-tick)))


noweb-start and noweb-end header args

2024-03-05 Thread Amy Grinn

I would like to add support for setting 'org-babel-noweb-wrap-start and
'org-babel-noweb-wrap-end for each src block individually using the
header args :noweb-start and :noweb-end:

#+name: firewall-safe-mode
#+begin_src sh
  echo "Firewall is now in safe mode."
#+end_src

#+name: firewall
#+begin_src sh  :noweb yes :noweb-start <<< :noweb-end >>>
  safe_mode () {
  echo "Error encountered, switching to safe mode."
  <<>>
  exit 1
  }

  setup-firewall || safe_mode
#+end_src

#+begin_src sh :noweb yes :noweb-start ":-) " :noweb-end " (-:" :tangle 
"test.sh"
  # Setup firewall
  :-) firewall (-:

  # Do other things
#+end_src


Here is a simple way to implement this feature.

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 4dcfbd3b0..0be19ff06 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -194,15 +194,17 @@ This string must include a \"%s\" which will be replaced by the results."
   :package-version '(Org . "9.1")
   :safe #'booleanp)
 
-(defun org-babel-noweb-wrap (&optional regexp)
+(defun org-babel-noweb-wrap (&optional regexp info)
   "Return regexp matching a Noweb reference.
 
 Match any reference, or only those matching REGEXP, if non-nil.
 
 When matching, reference is stored in match group 1."
-  (concat (regexp-quote org-babel-noweb-wrap-start)
+  (concat (regexp-quote (or (cdr (assq :noweb-start (nth 2 info)))
+org-babel-noweb-wrap-start))
 	  (or regexp "\\([^ \t\n]\\(?:.*?[^ \t\n]\\)?\\)")
-	  (regexp-quote org-babel-noweb-wrap-end)))
+	  (regexp-quote (or (cdr (assq :noweb-end (nth 2 info)))
+org-babel-noweb-wrap-end
 
 (defvar org-babel-src-name-regexp
   "^[ \t]*#\\+name:[ \t]*"
@@ -3116,7 +3118,7 @@ block but are passed literally to the \"example-block\"."
       (not (equal (cdr v) "no"))
 	 (noweb-re (format "\\(.*?\\)\\(%s\\)"
 			   (with-current-buffer parent-buffer
-			 (org-babel-noweb-wrap)
+			 (org-babel-noweb-wrap nil info)
 (unless (equal (cons parent-buffer
  (with-current-buffer parent-buffer
(buffer-chars-modified-tick)))


Re: [BUG]: contradictory requirements between resolving links and noweb result block

2024-02-05 Thread Ihor Radchenko
gerard.vermeu...@posteo.net writes:

> * mwe-noweb-ok
>
> ...
> Resolving the last link fails, because it has an org-id
> #+begin_src latex
> Listing \ref{noop}, \ref{make-noweb}, and \ref{orgd105392}.
> #+end_src
> while the caption of the linked listing has a user label.

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

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



Re: [BUG]: contradictory requirements between resolving links and noweb result block

2024-02-04 Thread gerard . vermeulen



On 02.02.2024 21:12, Ihor Radchenko wrote:

gerard.vermeu...@posteo.net writes:


I want to have a kind of dynamic RESULTS: noweb block to select what
other block to put in the LaTeX  preamble on LaTeX export.
I can get it working, but in this case links to the RESULTS: noweb 
block

are not resolved.  I found no way to get the links resolved without
breaking Noweb.  See my MWE with instructions below:


May you please provide more detailed information about your MWE?
What exactly are the steps? What did you expect to happen? What
happened instead?


I have turned my MWE into two almost identical MWE's (attached,
including a diff), so that LaTeX export output shows the problem.
I start with my expectations and then I explain what contains the
LaTeX output for mwe-noweb-ok.org and mwe-links-ok.org.

* LaTeX export objectives

Noweb should work, meaning that the LaTeX output preamble should
contain (above \author) the line:
#+begin_src latex
% Should go to the LaTeX preamble above \author{}.
#+end_src

Resolving links should work, meaning that the LaTeX output should
contain (below \tableofcontents) the line
#+begin_src latex
Listing \ref{noop}, \ref{make-noweb}, and \ref{make-noweb-result}.
#+end_src
where all links have user labels.

* mwe-noweb-ok

Export of mwe-noweb-ok.org to LaTeX shows that noweb works
Noweb works, because the preamble contains:
#+begin_src latex
% Should go to the LaTeX preamble above \author{}.
#+end_src

Resolving the last link fails, because it has an org-id
#+begin_src latex
Listing \ref{noop}, \ref{make-noweb}, and \ref{orgd105392}.
#+end_src
while the caption of the linked listing has a user label.

* mwe-links-ok

Noweb fails, because the LaTeX output preamble does contain
#+begin_src latex
\usepackage{hyperref}
\author{Gerard Vermeulen}
#+end_src
without the LaTeX comment.

Resolving links works, because the LaTeX output contains
#+begin_src latex
Listing \ref{noop}, \ref{make-noweb}, and \ref{make-noweb-result}.
#+end_src

* Problem

I can find no configuration where noweb and resolving the link
to the noweb block works.

Regards -- Gerard


mwe-noweb-ok.org
Description: Binary data


mwe-links-ok.org
Description: Binary data


mwe.diff
Description: Binary data


Re: [BUG]: contradictory requirements between resolving links and noweb result block

2024-02-02 Thread Ihor Radchenko
gerard.vermeu...@posteo.net writes:

> I want to have a kind of dynamic RESULTS: noweb block to select what
> other block to put in the LaTeX  preamble on LaTeX export.
> I can get it working, but in this case links to the RESULTS: noweb block
> are not resolved.  I found no way to get the links resolved without
> breaking Noweb.  See my MWE with instructions below:

May you please provide more detailed information about your MWE?
What exactly are the steps? What did you expect to happen? What happened 
instead?

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



[BUG]: contradictory requirements between resolving links and noweb result block

2024-02-02 Thread gerard . vermeulen

Hi,

I want to have a kind of dynamic RESULTS: noweb block to select what
other block to put in the LaTeX  preamble on LaTeX export.
I can get it working, but in this case links to the RESULTS: noweb block
are not resolved.  I found no way to get the links resolved without
breaking Noweb.  See my MWE with instructions below:

--- begin Org file ---
# -*- org-latex-prefer-user-labels: t -*- for easier understanding
Listing [[noop]], [[make-noweb]], and [[make-noweb-result]].
#+caption: NOOP
#+name: noop
#+begin_src latex :exports code
% I want to be in the LaTeX preamble above \author{}.
#+end_src
BUG: contradictory requirements for listing [[make-noweb-result]]:
1. Noweb works only in case [[make-noweb]] wraps [[make-noweb-result]] 
with

   *:exports both*.
2. Resolving links works only in case [[make-noweb]] wraps
   [[make-noweb-result]] with *:exports code*.
#+caption: Change after 1st :exports "both" to "code" and vice versa.
#+caption: Execute this block after changing, but before LaTeX export.
#+header: :wrap "src latex -n :exports both :noweb yes :results raw"
#+name: make-noweb
#+begin_src emacs-lisp :exports both :eval no-export
"#+latex_header: <>"
#+end_src
#+caption: I try to put listing [[noop]] body in the LaTeX preamble.
#+name: make-noweb-result
#+RESULTS: make-noweb
#+begin_src latex -n :exports both :noweb yes :results raw
,#+latex_header: <>
#+end_src
--- end Org file ---

I realize that this way of doing is contrived.
What I like about it, is that the [[make-noweb-results]] is directly
visible.  I can probably also modify `org-latex-packages-alist' but
that is less visible.

Regards -- Gerard





[FR] Re: language-specific org-babel-noweb-wrap-start?

2023-10-02 Thread Ihor Radchenko
Edgar Lux  writes:

> I wish you a nice day, week, month...
> I would like to know if there is a way to set =org-babel-noweb-wrap-start= 
> for a specific language. If I set that variable to a value, it will apply to 
> all of them. Thank you.
> ...
> What I would like is something like
>
> #+begin_src lang1 :noweb yes
> //;name_of_anoter_block;//
> #+end_src
>
> # Local Variables:
> # org-babel-noweb-wrap-start_lang1: "//;"
> # org-babel-noweb-wrap-end_lang1: ";//"
> # End:

This is currently not possible, but sounds like a reasonable addition.
Patches welcome. See https://orgmode.org/worg/org-contribute.html

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



language-specific org-babel-noweb-wrap-start?

2023-10-02 Thread Edgar Lux
Hi!

I wish you a nice day, week, month...
I would like to know if there is a way to set =org-babel-noweb-wrap-start= for 
a specific language. If I set that variable to a value, it will apply to all of 
them. Thank you.

This is fine right now
#+begin_src lang1 :noweb yes
//;name_of_anoter_block;//
#+end_src

# Local Variables:
# org-babel-noweb-wrap-start: "//;"
# org-babel-noweb-wrap-end: ";//"
# End:

What I would like is something like

#+begin_src lang1 :noweb yes
//;name_of_anoter_block;//
#+end_src

# Local Variables:
# org-babel-noweb-wrap-start_lang1: "//;"
# org-babel-noweb-wrap-end_lang1: ";//"
# End:

Thanks!

-- 
Sent with https://mailfence.com  
Secure and private email



Re: [BUG] Unable to detangle code with noweb comments [9.6.1 (9.6.1-??-fe92a3c @ /home/phtrivier/.emacs.d/.local/straight/build-28.1/org/)]

2023-04-24 Thread Ihor Radchenko
Pierre-Henri Trivier  writes:

> I'm using an org file like this one:
> ...
>     #+name: main
>     #+begin_src rust :tangle main.rs :noweb yes :comments noweb
>     <>
>     fn main() {
>     <>
>     <>
>     }
>     #+end_src
> When I visit the generated file, modify it, and I run
> org-babel-detangle, I'm execting the org file to be changed.
>
> Instead, I always get the error "Not in tangled code"

AFAIU, `org-babel-detangle' does not support nested noweb blocks.
The last discussion was in 
https://orgmode.org/list/20211017021707.horde.p9ib-mmooanjoruucbc5...@www.vfemail.net

Confirmed.

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



[BUG] Unable to detangle code with noweb comments [9.6.1 (9.6.1-??-fe92a3c @ /home/phtrivier/.emacs.d/.local/straight/build-28.1/org/)]

2023-04-24 Thread Pierre-Henri Trivier
I'm using an org file like this one:

    * Simu

    `simu` is a program to simulate counting up to a certain number.

    The program has two main parts:

    ** Gathering input

    We simply get the first parameter, panicking if there is nothing specified.

    #+name: gather-input
    #+begin_src rust :noweb yes :comments noweb
    let args: Vec = env::args().collect();
    if args.len() < 2 {
    panic!("No argument provided");
    }
    <>
    #+end_src

    This uses the `env` package, so we should add it:

    #+name: imports
    #+begin_src rust :noweb yes :comments link
    use std::env;
    #+end_src

    The input is provided as a string, so we need to parse, and report error if 
it fails.

    #+name: parse-input
    #+begin_src rust :noweb yes :comments link
    let count = args[1].parse::().unwrap();
    #+end_src

    ** Counting up to the number

    The loop is pretty straigforward:

    #+name: count
    #+begin_src rust :noweb yes :comments noweb
    let mut i = 0;
    while i < count {
    <>
    i = i+1;
    }
    #+end_src

    Displaying the counter is a matter of presentation:

    #+name: print-current-number
    #+begin_src rust :noweb yes :comments link
    println!("Current number is {}", i);
    #+end_src

    ** Tying things up

    #+name: main
    #+begin_src rust :tangle main.rs :noweb yes :comments noweb
    <>
    fn main() {
    <>
    <>
    }
    #+end_src

When using org-babel-tangle, a file is generated with this content:

    // [[file:simu.org::main][main]]
    // [[file:simu.org::imports][imports]]
    use std::env;
    // imports ends here
    fn main() {
    // [[file:simu.org::gather-input][gather-input]]
    let args: Vec = env::args().collect();
    if args.len() < 2 {
    panic!("No argument provided");
    }
    // [[file:simu.org::parse-input][parse-input]]
    let count = args[1].parse::().unwrap();
    // parse-input ends here
    // gather-input ends here
    // [[file:simu.org::count][count]]
    let mut i = 0;
    while i < count {
    // [[file:simu.org::print-current-number][print-current-number]]
    println!("Current number is {}", i);
    // print-current-number ends here
    i = i+1;
    }
    // count ends here
    }
    // main ends here

When I visit the generated file, modify it, and I run
org-babel-detangle, I'm execting the org file to be changed.

Instead, I always get the error "Not in tangled code"

If instead I annotate my blocks with :comments links , then I don't even
get any comments in the generated file.

I supposed it's related to using noweb expansion.

What am I doing wrong ?

Emacs  : GNU Emacs 28.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.33, 
cairo version 1.16.0)
 of 2022-05-31
Package: Org mode version 9.6.1 (9.6.1-??-fe92a3c @ 
/home/phtrivier/.emacs.d/.local/straight/build-28.1/org/)


Re: [BUG] Babel Noweb and python name clashing [9.6.1 (9.6.1-??-fe92a3ced @ /var/home/kwalerie/.emacs.d/.local/straight/build-27.1/org/)]

2023-04-23 Thread Ihor Radchenko
Weaver Marquez  writes:

> Expected Behaviour:
> The two code snippets below have the exact same code, except one has its
> Babel name as =savefig=, which is a function name inside its code
> block, and the other doesn't.
>
> ...
> ==---==
> #+name: doesnt-work

Works on my side.
Cannot reproduce.

Did you try with a clean config? See
https://orgmode.org/manual/Feedback.html#Feedback

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



[BUG] Babel Noweb and python name clashing [9.6.1 (9.6.1-??-fe92a3ced @ /var/home/kwalerie/.emacs.d/.local/straight/build-27.1/org/)]

2023-04-22 Thread Weaver Marquez
Following the Worg documentation for python babel linked
[[here][https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-python.html]],
I had a lot of trouble with the =savefig= related examples.

Expected Behaviour:
The two code snippets below have the exact same code, except one has its
Babel name as =savefig=, which is a function name inside its code
block, and the other doesn't.

Not sure if this is a documented issue, but figured I should mention.

==---==

#+name: savefig
#+begin_src python :var filename="images/python-matplot-figure.png"
return f"plt.savefig('{filename}')\nreturn '{filename}'"
#+end_src

#+name: substitute
#+begin_src python :var filename="images/python-matplot-figure.png"
return f"plt.savefig('{filename}')\nreturn '{filename}'"
#+end_src

==---==
#+name: doesnt-work
#+begin_src python :noweb yes :results file
import matplotlib, numpy
import matplotlib.pyplot as plt
#matplotlib.use('Agg')

fig=plt.figure(figsize=(4,2))
x=numpy.linspace(-15,15)

plt.plot(numpy.sin(x)/x)
fig.tight_layout()
<>
#+end_src

#+name: does-work
#+begin_src python :noweb yes :results file
import matplotlib, numpy
import matplotlib.pyplot as plt
#matplotlib.use('Agg')

fig=plt.figure(figsize=(4,2))
x=numpy.linspace(-15,15)

plt.plot(numpy.sin(x)/x)
fig.tight_layout()

<>
#+end_src


Emacs  : GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.30, cairo version 1.16.0)
 of 2022-01-24, modified by Debian
Package: Org mode version 9.6.1 (9.6.1-??-fe92a3ced @
/var/home/kwalerie/.emacs.d/.local/straight/build-27.1/org/)

current state:
==
(setq
 org-src-mode-hook '(org-src-babel-configure-edit-buffer
org-src-mode-configure-edit-buffer)
 org-fontify-whole-heading-line t
 org-link-shell-confirm-function 'yes-or-no-p
 org-mode-local-vars-hook '(+org-init-gifs-h)
 org-babel-after-execute-hook '(+org-redisplay-inline-images-in-babel-result-h)
 org-insert-heading-respect-content t
 org-after-refile-insert-hook '(save-buffer)
 org-metadown-hook '(org-babel-pop-to-session-maybe)
 org-follow-link-hook '(+nav-flash-delayed-blink-cursor-h)
 org-persist-directory "/var/home/kwalerie/.emacs.d/.local/cacheorg/persist/"
 org-refile-targets '((nil :maxlevel . 3) (org-agenda-files :maxlevel . 3))
 org-html-format-inlinetask-function
'org-html-format-inlinetask-default-function
 org-enforce-todo-dependencies t
 org-odt-format-headline-function 'org-odt-format-headline-default-function
 org-special-ctrl-a/e t
 org-imenu-depth 6
 org-persist-before-write-hook '(org-element--cache-persist-before-write)
 org-agenda-files '("/var/home/kwalerie/org/todo.org"
"/var/home/kwalerie/org/2023-02-06-healthsci.org"
"/var/home/kwalerie/org/battery.org"
"/var/home/kwalerie/org/draft.org")
 org-ascii-format-inlinetask-function 'org-ascii-format-inlinetask-default
 org-reveal-start-hook '(org-decrypt-entry)
 org-modules '(ol-bibtex)
 org-startup-folded nil
 org-blocker-hook '(org-block-todo-from-children-or-siblings-or-parent)
 org-mode-hook '(er/add-org-mode-expansions +lookup--init-org-mode-handlers-h
 (closure ((hook . org-mode-hook) (--dolist-tail--) t)
(&rest _) (add-hook 'before-save-hook 'org-encrypt-entries nil t))
 #[0 "\300\301\302\303\304$\207" [add-hook
change-major-mode-hook org-fold-show-all append local] 5]
 #[0 "\300\301\302\303\304$\207" [add-hook
change-major-mode-hook org-babel-show-result-all append local] 5]
 org-babel-result-hide-spec org-babel-hide-all-hashes
#[0 "\301\211\207" [imenu-create-index-function org-imenu-get-tree] 2]
 doom-disable-show-paren-mode-h
doom-disable-show-trailing-whitespace-h +org-make-last-point-visible-h
evil-org-mode toc-org-enable
 embrace-org-mode-hook org-eldoc-load)
 org-clock-persist 'history
 org-export-with-smart-quotes t
 org-odt-format-drawer-function #[514 "\207" [] 3 "\n\n(fn NAME CONTENTS)"]
 org-outline-path-complete-in-steps nil
 org-archive-hook '(org-attach-archive-delete-maybe)
 org-persist-before-read-hook '(org-element--cache-persist-before-read)
 org-agenda-finalize-hook
'(+org-exclude-agenda-buffers-from-workspace-h
+org-defer-mode-in-agenda-buffers-h)
 org-startup-indented t
 org-clock-history-length 20
 org-agenda-before-write-hook '(org-agenda-add-entry-text)
 org-metaup-hook '(org-babel-load-in-session-maybe)
 org-persist-after-read-hook '(org-element--cache-persist-after-read)
 org-bibtex-headline-format-function #[257 "\300\236A\207" [:title] 3
"\n\n(fn ENTRY)"]
 org-latex-format-drawer-function #[514 "\207" [] 3 "\n\n(fn _ CONTENTS)"]
 org-agenda

Re: [O] Injecting properties with noweb

2023-04-08 Thread Ken Mankoff
Hi Ihor,

I had posted this in another thread, but repeat it here for anyone interested. 
I think it is a similar end result to what you posted, but skips the =identity= 
 step. The examples below show (1) setting a bash environment variable in 
screen, or (2) printing from a Python prompt after sshing to a remote computer. 
It is language agnostic. Because it uses PROPERTIES and not :var, it also lets 
me work in Org Column View mode.


* Header
:PROPERTIES:
:foo: 42
:END:

#+NAME: ex1-screen-bash
#+BEGIN_SRC screen

export foo="<>"
#+END_SRC

#+NAME: ex2-ssh-python
#+BEGIN_SRC bash

ssh somewhere
python
print("<>")
#+END_SRC

#+CALL: ex2-ssh-python()

#+RESULTS:
: foo


The relevant section from my library-of-babel is:

* Properties into header args
:PROPERTIES:
:hellomessage: hello
:END:

https://emacs.stackexchange.com/questions/41922/

#+NAME: get_property
#+BEGIN_SRC emacs-lisp :var prop_name="" :results silent

(org-with-point-at org-babel-current-src-block-location
  (org-entry-get nil prop_name t))
#+END_SRC

** Example Usage

*** Header arg

#+HEADER: :var prop_message=(org-entry-get nil "hellomessage" t)
#+BEGIN_SRC emacs-lisp
  (message prop_message)
#+END_SRC

#+RESULTS:
: hello

*** Noweb

#+BEGIN_SRC emacs-lisp :noweb yes
  (message "<>")
#+END_SRC


#+RESULTS:
: hello

#+BEGIN_SRC bash :noweb yes :results verbatim
echo "<>"
#+END_SRC

#+RESULTS:
: hello

If hope this helps someone if they need it.

  -k.




On 2023-04-08 at 05:22 -07, Ihor Radchenko  wrote...
> Ken Mankoff  writes:
>
>> Is it possible to set variables using Org Babel inside screen, which
>> does not support ":var" header args? I'd actually lke a double-nested
>> screen over ssh, and the ability to re-use Babel code blocks under
>> different headings, using header args or PROPERTIES to change
>> variables. That is, something like:
>
> Yes.
>
>> ...
>> #+NAME: get-prop
>> #+BEGIN_SRC emacs-lisp :var prop="FOO" :noweb yes
>> (org-macro--get-property prop "")
>> #+END_SRC
>>
>> #+NAME: inject_vars
>> #+BEGIN_SRC shell :noweb yes
>> # echo <> # testing
>> echo export FOO=<>
>> echo export BAR=<>
>> echo export BAZ=<>
>> #+END_SRC
>
> This did not work as you expected because noweb evaluates code block
> with point at that code block.
>
> To get the property value at the code block where you expand noweb
> reference, you need to compute the value in the arguments to the
> reference. Something like
>
> #+name: identity
> #+begin_src elisp :var x=""
>
> x
> #+end_src
>
> ...
> echo export FOO=< ...




Re: [O] Injecting properties with noweb

2023-04-08 Thread Ihor Radchenko
Ken Mankoff  writes:

> Is it possible to set variables using Org Babel inside screen, which does not 
> support ":var" header args? I'd actually lke a double-nested screen over ssh, 
> and the ability to re-use Babel code blocks under different headings, using 
> header args or PROPERTIES to change variables. That is, something like:

Yes.

> ...
> #+NAME: get-prop
> #+BEGIN_SRC emacs-lisp :var prop="FOO" :noweb yes
> (org-macro--get-property prop "")
> #+END_SRC
>
> #+NAME: inject_vars
> #+BEGIN_SRC shell :noweb yes
> # echo <> # testing
> echo export FOO=<>
> echo export BAR=<>
> echo export BAZ=<>
> #+END_SRC

This did not work as you expected because noweb evaluates code block
with point at that code block.

To get the property value at the code block where you expand noweb
reference, you need to compute the value in the arguments to the
reference. Something like

#+name: identity
#+begin_src elisp :var x=""
x
#+end_src

...
echo export FOO=<https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>



Re: Noweb Function's body without evaluation

2023-04-04 Thread Ihor Radchenko
suarezmigu...@icloud.com writes:

> #+name: getClientInstanceNameNew
> #+begin_src shell :session something :var connection="admin@10.0.3.149" :var 
> client="example_client" :var apacheDir="/etc/apache/vhosts"
> <>
> client=$client
> apacheDir=$apacheDir
> grep $client $apacheDir/*
> #+end_src
>
> So, the initSSH call works successfully, so that concludes my first question, 
> being that I can now affect the $connection variable. Thank you again!
>
> However, since the SSH call changes the environment, the $client and 
> $apacheDir variables are not defined in the new environment. I did try 
> setting it again like above, but this doesn’t help as the variables do not 
> exist.

Org babel knows not if the environment changes along the way.
Of course, you can break the variable in bash by calling "bash" as one of
the commands in src block. Or you can do more crazy staff and call
"python" (or, say, "ghci") interpreter. There is no sane way to handle
such weird scenarios programmatically.

I suggest you to write some kind of wrapper like

#+name: obl-identity
#+begin_src emacs-lisp :var x=""
x
#+end_src

#+begin_src bash :noweb yes
<>
client=<>
#+end_src

This will not rely on language-specific way to define variables and
instead generate the code you want to execute fully, on Elisp side.

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



Re: Noweb Function's body without evaluation

2023-04-03 Thread suarezmiguelc
Wow, works!, thank you very much.

Now, I have a more specific example.

#+name: initSSH
#+begin_src shell :var connection=“admin@10.0.3.200" :noweb yes
ssh -t miguel@172.28.3.249 "sudo -u admin ssh -t $connection 'sudo su'"
#+end_src

#+name: getClientInstanceNameNew
#+begin_src shell :session something :var connection="admin@10.0.3.149" :var 
client="example_client" :var apacheDir="/etc/apache/vhosts"
<>
client=$client
apacheDir=$apacheDir
grep $client $apacheDir/*
#+end_src

So, the initSSH call works successfully, so that concludes my first question, 
being that I can now affect the $connection variable. Thank you again!

However, since the SSH call changes the environment, the $client and $apacheDir 
variables are not defined in the new environment. I did try setting it again 
like above, but this doesn’t help as the variables do not exist.

 99% of the commands I run are remote so, I will keep investigating this. It 
would make more sense to use :dir and tramp, but as the connections are slow, I 
can only connect with this call which works pretty fast.

> 2/4/23 12:06、Ihor Radchenko のメール:
> 
> suarezmigu...@icloud.com writes:
> 
>> I use heavily org-mode for Literate DevOps, so I have a lot of shell 
>> commands that connect through SSH and do some things later, for example:
>> 
>> #+name: initSSH
>> #+begin_src shell :var connection=“admin@somehost"
>> ssh -t miguel@host "sudo -u someuser ssh -t $connection 'sudo su'"
>> #+end_src
>> 
> p ...>
>> #+name: getStorage
>> #+begin_src shell
>> df
>> #+end_src
>> 
>> Which has to be run in a remote server, could be any remote server as I have 
>> to connect to several. So I would like to be able to:
>> 
>> #+begin_src shell
>> <>
>> <>
>> #+end_src
> 
> You can just
> 
> #+begin_src shell :var connection "admin@anotherhost"
> <>
> >
> #+end_src
> 
> -- 
> 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>




Re: Noweb Function's body without evaluation

2023-04-02 Thread Ihor Radchenko
suarezmigu...@icloud.com writes:

> I use heavily org-mode for Literate DevOps, so I have a lot of shell commands 
> that connect through SSH and do some things later, for example:
>
> #+name: initSSH
> #+begin_src shell :var connection=“admin@somehost"
> ssh -t miguel@host "sudo -u someuser ssh -t $connection 'sudo su'"
> #+end_src
>
p ...>
> #+name: getStorage
> #+begin_src shell
> df
> #+end_src
>
> Which has to be run in a remote server, could be any remote server as I have 
> to connect to several. So I would like to be able to:
>
> #+begin_src shell
> <>
> <>
> #+end_src

You can just

#+begin_src shell :var connection "admin@anotherhost"
<>
>
#+end_src

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: Noweb Function's body without evaluation

2023-03-16 Thread suarezmiguelc
Hello Ken, thank you for your message,

After reading the very interesting get_property function, I found that even 
though I will probably use it for some cases, it doesn’t apply directly, to my 
case.

For more examples, if I have 1 source code block:

>> #+name: greeting
>> #+begin_src sh :var name="world" :results output :session testing
>> 
>> echo "hello, $name\!"
>> #+end_src


I have three options in noweb to use this:
- Use its body into another begin_src source code block with <>
- Use its result “hello, world!” Into another code block, which results in 
babel trying to execute the hello, command, which doesn’t exist, this with 
<>
- Use its result, the same as above, but with another parameter, results in the 
same but the variable name is different, so <>

I’m trying to do the first, but with another parameter, so Use its body into 
another begin_src source code block with, get the resulting body after changing 
the variable without it being evaluated, so that I get a valid command to get 
to bash, like with <>, but I can specify a different name variable.

I found the [:body] param but, even though it lets me change the variable as I 
want, it then tries to evaluate it, so I get a different value but the same 
hello, command doesn’t exist. Even though what I would want is to get echo 
“hello, $name\!” but, $name is different.

Thank you for your response Ken, I hope I gave a clearer example with the above.


> 16/3/23 5:16、Ken Mankoff のメール:
> 
> Hi,
> 
> I'm not sure that I understand your issue or needs from the provided 
> examples, but I wonder if the example I provide here would be helpful. It 
> bypasses :var an lets you inject a PROPERTY value anywhere. It is also 
> language agnostic. You can use it to execute commands (that are set as 
> PROPERTY values) or set variables to values.
> 
> https://lists.gnu.org/archive/html/emacs-orgmode/2023-03/msg00251.html
> 
>  -k.
> 
> On 2023-03-15 at 18:54 -04, suarezmigu...@icloud.com wrote...
>> Hello Org-mode community. I’m using Emacs Doom Framework, specifically:
>> 
>> Emacs 28.2 (build 1, aarch64-apple-darwin22.3.0, Carbon Version 169
>> AppKit 2299.4) of 2023-02-23.
>> 
>> I use heavily org-mode for Literate DevOps, so I have a lot of shell
>> commands that connect through SSH and do some things later, for
>> example:
>> 
>> #+name: initSSH
>> #+begin_src shell :var connection=“admin@somehost"
>> 
>> ssh -t miguel@host "sudo -u someuser ssh -t $connection 'sudo su'"
>> #+end_src
>> 
>> So then I can call:
>> 
>> #+call: initSSH(connection=“admin@anotherhost”)
>> 
>> With any other header parameters or session, the above works
>> correctly. I cannot use tramp due to network latency issues, so this
>> is the most performance way for me, since I also have to do some
>> multi-hops which are indeed supported in tramp, but it is too slow for
>> me, so I rather only commands.
>> 
>> The thing is that, I then would like to call these not with a #+call
>> function, but add them into a bigger script, let’s say that I define
>> another command:
>> 
>> #+name: getStorage
>> #+begin_src shell
>> 
>> df
>> #+end_src
>> 
>> Which has to be run in a remote server, could be any remote server as
>> I have to connect to several. So I would like to be able to:
>> 
>> #+begin_src shell
>> <>
>> <>
>> #+end_src
>> 
>> 
>> The first doesn’t work as org-mode runs the code and passes the
>> resulting string to bash, which isn’t a command. The latter works
>> normally. So the issue here are the parameters.
>> 
>> So I made another simple example for this:
>> 
>> #+name: greeting
>> #+begin_src sh :var name="world" :results output :session testing
>> 
>> echo "hello, $name\!"
>> #+end_src
>> 
>> #+results: greeting
>> #+begin_src sh
>> 
>> hello, world\!
>> #+end_src
>> 
>> #+begin_src shell
>> <>
>> #+end_src
>> 
>> 
>> This results in sh: hello,: command not found, as it is executing the 
>> function. I see in the documentation that I can:
>> - Call a function’s body with <>
>> - Execute a function and return its results with <>
>> - Execute a function and return its results even with different params with 
>> <>
>> 
>> So right now, the one that’s missing is, call a function’s body with 
>> different parameters. So the
>> function <> is not evaluated.
>> 
>> After searching a lot, I came a

Re: Noweb Function's body without evaluation

2023-03-15 Thread Ken Mankoff
Hi,

I'm not sure that I understand your issue or needs from the provided examples, 
but I wonder if the example I provide here would be helpful. It bypasses :var 
an lets you inject a PROPERTY value anywhere. It is also language agnostic. You 
can use it to execute commands (that are set as PROPERTY values) or set 
variables to values.

https://lists.gnu.org/archive/html/emacs-orgmode/2023-03/msg00251.html

  -k.

On 2023-03-15 at 18:54 -04, suarezmigu...@icloud.com wrote...
> Hello Org-mode community. I’m using Emacs Doom Framework, specifically:
>
> Emacs 28.2 (build 1, aarch64-apple-darwin22.3.0, Carbon Version 169
> AppKit 2299.4) of 2023-02-23.
>
> I use heavily org-mode for Literate DevOps, so I have a lot of shell
> commands that connect through SSH and do some things later, for
> example:
>
> #+name: initSSH
> #+begin_src shell :var connection=“admin@somehost"
>
> ssh -t miguel@host "sudo -u someuser ssh -t $connection 'sudo su'"
> #+end_src
>
> So then I can call:
>
> #+call: initSSH(connection=“admin@anotherhost”)
>
> With any other header parameters or session, the above works
> correctly. I cannot use tramp due to network latency issues, so this
> is the most performance way for me, since I also have to do some
> multi-hops which are indeed supported in tramp, but it is too slow for
> me, so I rather only commands.
>
> The thing is that, I then would like to call these not with a #+call
> function, but add them into a bigger script, let’s say that I define
> another command:
>
> #+name: getStorage
> #+begin_src shell
>
> df
> #+end_src
>
> Which has to be run in a remote server, could be any remote server as
> I have to connect to several. So I would like to be able to:
>
> #+begin_src shell
> <>
> <>
> #+end_src
>
>
> The first doesn’t work as org-mode runs the code and passes the
> resulting string to bash, which isn’t a command. The latter works
> normally. So the issue here are the parameters.
>
> So I made another simple example for this:
>
> #+name: greeting
> #+begin_src sh :var name="world" :results output :session testing
>
> echo "hello, $name\!"
> #+end_src
>
> #+results: greeting
> #+begin_src sh
>
> hello, world\!
> #+end_src
>
> #+begin_src shell
> <>
> #+end_src
>
>
> This results in sh: hello,: command not found, as it is executing the 
> function. I see in the documentation that I can:
> - Call a function’s body with <>
> - Execute a function and return its results with <>
> - Execute a function and return its results even with different params with 
> <>
>
> So right now, the one that’s missing is, call a function’s body with 
> different parameters. So the
> function <> is not evaluated.
>
> After searching a lot, I came across:
>
> #+begin_src shell :session testing
> <>
> #+end_src
>
> Which results in:
>
> sh-3.2$ PS1="org_babel_sh_prompt> "
> org_babel_sh_prompt> name='Testin'
> org_babel_sh_prompt> echo "hello, $name\!"
> hello, Testin\!
> org_babel_sh_prompt> echo 'org_babel_sh_eoe'
> org_babel_sh_eoe
> org_babel_sh_prompt> hello, Testin\!
> sh: hello,: command not found
> org_babel_sh_prompt> echo 'org_babel_sh_eoe'
> org_babel_sh_eoe
> org_babel_sh_prompt> 
>
> Which is somewhat what I need since at least the variable is changed,
> but the result of this execution is also passed to shell so, same
> error.
>
> I can’t find much documentation about this, what is the correct syntax
> here?,
>
> Thank you!




Noweb Function's body without evaluation

2023-03-15 Thread suarezmiguelc


Hello Org-mode community. I’m using Emacs Doom Framework, specifically:

Emacs 28.2 (build 1, aarch64-apple-darwin22.3.0, Carbon Version 169 AppKit 
2299.4) of 2023-02-23.

I use heavily org-mode for Literate DevOps, so I have a lot of shell commands 
that connect through SSH and do some things later, for example:

#+name: initSSH
#+begin_src shell :var connection=“admin@somehost"
ssh -t miguel@host "sudo -u someuser ssh -t $connection 'sudo su'"
#+end_src

So then I can call:

#+call: initSSH(connection=“admin@anotherhost”)

With any other header parameters or session, the above works correctly. I 
cannot use tramp due to network latency issues, so this is the most performance 
way for me, since I also have to do some multi-hops which are indeed supported 
in tramp, but it is too slow for me, so I rather only commands.

The thing is that, I then would like to call these not with a #+call function, 
but add them into a bigger script, let’s say that I define another command:

#+name: getStorage
#+begin_src shell
df
#+end_src

Which has to be run in a remote server, could be any remote server as I have to 
connect to several. So I would like to be able to:

#+begin_src shell
<>
<>
#+end_src

The first doesn’t work as org-mode runs the code and passes the resulting 
string to bash, which isn’t a command. The latter works normally. So the issue 
here are the parameters.

So I made another simple example for this:

#+name: greeting
#+begin_src sh :var name="world" :results output :session testing
echo "hello, $name\!"
#+end_src

#+results: greeting
#+begin_src sh
hello, world\!
#+end_src

#+begin_src shell
<>
#+end_src

This results in sh: hello,: command not found, as it is executing the function. 
I see in the documentation that I can:
- Call a function’s body with <>
- Execute a function and return its results with <>
- Execute a function and return its results even with different params with 
<>

So right now, the one that’s missing is, call a function’s body with different 
parameters. So the function <> is not evaluated.

After searching a lot, I came across:

#+begin_src shell :session testing
<>
#+end_src

Which results in:

sh-3.2$ PS1="org_babel_sh_prompt> "
org_babel_sh_prompt> name='Testin'
org_babel_sh_prompt> echo "hello, $name\!"
hello, Testin\!
org_babel_sh_prompt> echo 'org_babel_sh_eoe'
org_babel_sh_eoe
org_babel_sh_prompt> hello, Testin\!
sh: hello,: command not found
org_babel_sh_prompt> echo 'org_babel_sh_eoe'
org_babel_sh_eoe
org_babel_sh_prompt> 

Which is somewhat what I need since at least the variable is changed, but the 
result of this execution is also passed to shell so, same error.

I can’t find much documentation about this, what is the correct syntax here?,

Thank you!


Re: Multiple noweb-ref

2023-03-03 Thread Ihor Radchenko
Théo Maxime Tyburn  writes:

>> + is a bit awkward.
>> Space would be more logical as separator.
>> Though I am wondering if people are using noweb reference names with
>> spaces in the wild.
>
> Might be. So maybe we could use another non-alphabetical character? What
> about "|" ?

I do not feel like being creative with characters here is a good idea.
I think that using multiple :noweb-ref+ will be more reliable and surely
will not break any existing configs.

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



Re: Multiple noweb-ref

2023-02-26 Thread Théo Maxime Tyburn
Hi Ihor,

>> Anyway I tried to hack my way trough it. It seems there are two things
>> to do :
>> 1) Enable noweb-ref to contain multiple references.
>> 2) Accumulate references when using header-args+ or use tags to set the
>> value of noweb-ref
>>
>> I came up with a quick patch for 1):
>> modified   lisp/ob-core.el
>> @@ -2910,8 +2910,11 @@ block but are passed literally to the 
>> \"example-block\"."
>>  (if (org-in-commented-heading-p)
>>  (org-forward-heading-same-level nil t)
>>(let* ((info (org-babel-get-src-block-info t))
>> - (ref (cdr (assq :noweb-ref (nth 2 info)
>> -    (push info (gethash ref cache))
>> + (refs (cdr (assq :noweb-ref (nth 2 info)
>> +(if refs
>> +(dolist (ref (s-split "+" refs))
>> +  (push info (gethash ref cache)))
>> +  (push info (gethash refs cache)))
>
> + is a bit awkward.
> Space would be more logical as separator.
> Though I am wondering if people are using noweb reference names with
> spaces in the wild.

Might be. So maybe we could use another non-alphabetical character? What
about "|" ?

>> For 2) I didn't check in detail how one could achieve this. I have the
>> impression it would be easier to use tags. One could define a
>> new variable `org-babel-set-noweb-refs-from-tags` that would be used in
>> `org-babel-get-src-block-info` to generate the value of noweb-ref we
>> would like to have depending on the tags of the headline of the
>> block. I'll try this soonish.
>
> I do not like the idea of using tags.
>
> What we might do is:
>
> 1. Leave :noweb-ref's current behavior of overwriting the parent
>parameter values.
> 2. Add a new :noweb-ref+ parameter to accumulate multiple noweb
>reference names. The relevant function to modify is
>`org-babel-merge-params'

I like the idea. Thanks for the hint, I'll try that out!

Best,

Théo



[O] Injecting properties with noweb

2023-02-20 Thread Ken Mankoff
Hello,

Is it possible to set variables using Org Babel inside screen, which does not 
support ":var" header args? I'd actually lke a double-nested screen over ssh, 
and the ability to re-use Babel code blocks under different headings, using 
header args or PROPERTIES to change variables. That is, something like:

* Setup
:SETTINGS:
:FOO: default
:BAR: one
:header-args:screen+: :cmd /bin/bash :session (org-macro--get-property "FOO" "")
:END:

#+NAME: setup
#+BEGIN_SRC screen
if [[ ! $(hostname) =~ "host"* ]]; then ssh host; fi
<>
# eval <> ??
echo $FOO
#+END_SRC

Should print out "one" (the default setting under Setup) in the screen terminal.

** OTHER
:PROPERTIES:
:FOO: two
:END:

#+BEGIN_SRC screen
<>
#+END_SRC

Should print out "two" (the sub-heding adjusted property) in the screen 
terminal.


I'm OK with not being able to inject arbitrary variables, only the ~10 or so 
that I need to be able to set, and having a code block that has these 10 
hard-coded on the LHS, but with some <> or something on the RHS so that 
they value of the variable can be controlled using PROPERTIES under headings 
(better yet, header-args, but I don't think that is possible).  

That's the behavior I'm after, but am having trouble. I thought something like:

#+NAME: get-prop
#+BEGIN_SRC emacs-lisp :var prop="FOO" :noweb yes
(org-macro--get-property prop "")
#+END_SRC

#+NAME: inject_vars
#+BEGIN_SRC shell :noweb yes
# echo <> # testing
echo export FOO=<>
echo export BAR=<>
echo export BAZ=<>
#+END_SRC

#+BEGIN_SRC screen
<>
echo $FOO
#+END_SRC

might work, but it's just printing nil.


Thanks for any suggestions,

  -k.



Re: Multiple noweb-ref

2023-02-20 Thread Ihor Radchenko
Théo Maxime Tyburn  writes:

> What I would like to have, when I expand `<>` is to get all the blocks
> under A, and when I expand `<>` I get all the blocks under B. For
> now when I expand `<>` I only get the blocks under A that are not under 
> B.
>
> Is there a way to do this with the current features of org-babel?

No, AFAIK. Not easily at least.

> Anyway I tried to hack my way trough it. It seems there are two things
> to do :
> 1) Enable noweb-ref to contain multiple references.
> 2) Accumulate references when using header-args+ or use tags to set the
> value of noweb-ref
>
> I came up with a quick patch for 1):
> modified   lisp/ob-core.el
> @@ -2910,8 +2910,11 @@ block but are passed literally to the 
> \"example-block\"."
>   (if (org-in-commented-heading-p)
>   (org-forward-heading-same-level nil t)
> (let* ((info (org-babel-get-src-block-info t))
> -  (ref (cdr (assq :noweb-ref (nth 2 info)
> - (push info (gethash ref cache))
> +  (refs (cdr (assq :noweb-ref (nth 2 info)
> +(if refs
> +(dolist (ref (s-split "+" refs))
> +   (push info (gethash ref cache)))
> +  (push info (gethash refs cache)))

+ is a bit awkward.
Space would be more logical as separator.
Though I am wondering if people are using noweb reference names with
spaces in the wild.

> Feedback on the code is of course very welcome. Not sure if using a plus
> sign as a delimiter is clever. Also not sure if using `s-split` is a
> good idea, what would be the builtin alternative?

There is built-in `split-string'.

> For 2) I didn't check in detail how one could achieve this. I have the
> impression it would be easier to use tags. One could define a
> new variable `org-babel-set-noweb-refs-from-tags` that would be used in
> `org-babel-get-src-block-info` to generate the value of noweb-ref we
> would like to have depending on the tags of the headline of the
> block. I'll try this soonish.

I do not like the idea of using tags.

What we might do is:

1. Leave :noweb-ref's current behavior of overwriting the parent
   parameter values.
2. Add a new :noweb-ref+ parameter to accumulate multiple noweb
   reference names. The relevant function to modify is
   `org-babel-merge-params'

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



Multiple noweb-ref

2023-02-10 Thread Théo Maxime Tyburn
Hi,

I have several code blocks that I label using `:noweb-ref label`. I set
them for all blocks under a subtree by setting the org property
`header-args+` of the root of the subtree to `:noweb-ref label`.

Now with this approach, I can't accumulate `noweb-ref` like I could do for
tags. I would like all blocks under a subtree A to have the
reference "foo" and all the blocks under a subtree B of subtree A to
have the reference "bar", while also having the reference "foo".

What I would like to have, when I expand `<>` is to get all the blocks
under A, and when I expand `<>` I get all the blocks under B. For
now when I expand `<>` I only get the blocks under A that are not under B.

Is there a way to do this with the current features of org-babel?

Anyway I tried to hack my way trough it. It seems there are two things
to do :
1) Enable noweb-ref to contain multiple references.
2) Accumulate references when using header-args+ or use tags to set the
value of noweb-ref

I came up with a quick patch for 1):
modified   lisp/ob-core.el
@@ -2910,8 +2910,11 @@ block but are passed literally to the \"example-block\"."
(if (org-in-commented-heading-p)
(org-forward-heading-same-level nil t)
  (let* ((info (org-babel-get-src-block-info t))
-(ref (cdr (assq :noweb-ref (nth 2 info)
-   (push info (gethash ref cache))
+(refs (cdr (assq :noweb-ref (nth 2 info)
+(if refs
+(dolist (ref (s-split "+" refs))
+ (push info (gethash ref cache)))
+  (push info (gethash refs cache)))
 (funcall expand-references id cache)
 ;; Interpose PREFIX between every line.
     (mapconcat #'identity

With this I already get interesting results. A noweb-ref "foo+bar" gets
expanded by <> by and <>. For example if I define references like
this

#+BEGIN_SRC emacs-lisp :noweb-ref one
aaa
#+END_SRC
#+BEGIN_SRC emacs-lisp :noweb-ref two
bbb
#+END_SRC
#+BEGIN_SRC emacs-lisp :noweb-ref one+two
ababab
#+END_SRC

and tangle them like this

#+BEGIN_SRC emacs-lisp :tangle one.el :noweb yes
<>
#+END_SRC
#+BEGIN_SRC emacs-lisp :tangle two.el :noweb yes
<>
#+END_SRC

I get two files
one.el:
aaa
ababab

and two.el:
bbb
ababab

This is already nice!
Feedback on the code is of course very welcome. Not sure if using a plus
sign as a delimiter is clever. Also not sure if using `s-split` is a
good idea, what would be the builtin alternative?

For 2) I didn't check in detail how one could achieve this. I have the
impression it would be easier to use tags. One could define a
new variable `org-babel-set-noweb-refs-from-tags` that would be used in
`org-babel-get-src-block-info` to generate the value of noweb-ref we
would like to have depending on the tags of the headline of the
block. I'll try this soonish.

Best,

Théo



Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-12-27 Thread Ihor Radchenko
Ihor Radchenko  writes:

> Also, several small comments about the commit message.

For the record, an amended version of this patch have been applied.
See https://orgmode.org/list/87leona2r1.fsf@localhost

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



[FR] Allow noweb references from external files

2022-11-14 Thread Ihor Radchenko
Alexander Dinges  writes:

> Tangling a noweb src-block which is in another file does not work. See
> https://www.reddit.com/r/orgmode/comments/tb9reu/how_to_tangle_a_code_block_containing_noweb_from/
> and
> https://stackoverflow.com/questions/47058372/in-org-mode-how-to-call-code-block-to-evaluate-from-other-org-file/61580716#61580716

I agree that supporting noweb references from external files is a
reasonable feature to add.

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



[BUG] org-babel-expand-noweb-references works only with () when using external file [9.5.5 (9.5.5-g8ef620 @ /nix/store/k6fdlc7zjkpcc7pcgv4rzg6z03qcxhd3-emacs-packages-deps/share/emacs/site-lisp/elpa/o

2022-11-13 Thread Alexander Dinges



Tangling a noweb src-block which is in another file does not work. See
https://www.reddit.com/r/orgmode/comments/tb9reu/how_to_tangle_a_code_block_containing_noweb_from/
and
https://stackoverflow.com/questions/47058372/in-org-mode-how-to-call-code-block-to-evaluate-from-other-org-file/61580716#61580716


---
-



Emacs  : GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.34, cairo version 1.16.0)
Package: Org mode version 9.5.5 (9.5.5-g8ef620 @
/nix/store/k6fdlc7zjkpcc7pcgv4rzg6z03qcxhd3-emacs-packages-
deps/share/emacs/site-lisp/elpa/org-9.5.5/)




Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-11-01 Thread Ihor Radchenko
Daniel Ziltener  writes:

>> Could you please create a separate patch file and attach it in the
>> reply?
> Sure, here's the patch attached, as generated by "git format-patch 
> origin/main".

Thanks!

I tried to apply your patch and run tests. The new test for
"strip-tangle" is failing on my side.

Also, several small comments about the commit message.

> * lisp/ob-tangle.el (org-babel-tangle-single-block): strip noweb tags
> from block if :noweb has been set to strip-tangle.
> * lisp/ob-core.el (org-babel-common-header-args-w-values): add
> "strip-tangle" as new allowed value.
> * testing/lisp/test-ob-tangle.el (ob-tangle/strip-tangle): add new test
> case for strip-tangle.
> * doc/org-manual.org (Noweb Reference Syntax): adjust documentation for
> the noweb header argument.
> * etc/ORG-NEWS: add entry for new header argument value.

Please start sentences with capital letter.  Also, use "strip-tangle" or
strip-tangle consistently.

> This patch adds the "strip-tangle" option for the :noweb header
> argument. This strips the noweb tags before tangling the block. This can
  ^"  "

We use double space between sentences in commit messages as well. Not
only in the documentation.

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



Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-30 Thread Timothy
Hi Ihor,

> #+name: setup
> #+begin_src bash
>
> cd /path/to/tests
> #+end_src
>
> #+begin_src bash :noweb strip-tangle
> 
> make test1
> #+end_src
>
> #+begin_src bash :noweb strip-tangle
> 
> make test2
> #+end_src
>
> Then, one can interactively run individual tests from Org and then
> tangle the whole file into batch test script.

Ah, this makes sense to me now. Thanks for the example!

All the best,
Timothy

-- 
Timothy (‘tecosaur’/‘TEC’), 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/tec>.


Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-30 Thread Ihor Radchenko
Timothy  writes:

> Hi Daniel,
>
>> Add a “strip-tangle” noweb option to strip the noweb tags when tangling, but
>> keep and expand them otherwise.
>
> I must admit I can’t see the point of this — would you mind providing an
> example of when this would be useful?

#+name: setup
#+begin_src bash
cd /path/to/tests
#+end_src

#+begin_src bash :noweb strip-tangle
<>
make test1
#+end_src

#+begin_src bash :noweb strip-tangle
<>
make test2
#+end_src

Then, one can interactively run individual tests from Org and then
tangle the whole file into batch test script.

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



Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-30 Thread Daniel Ziltener


Am 30.10.22 um 05:12 schrieb Ihor Radchenko:

Daniel Ziltener  writes:


From: Daniel Ziltener 

* lisp/ob-tangle.el (org-babel-tangle-single-block): strip noweb tags
from block if :noweb has been set to strip-tangle.
* lisp/ob-core.el (org-babel-common-header-args-w-values): add
"strip-tangle" as new allowed value.
* testing/lisp/test-ob-tangle.el (ob-tangle/strip-tangle): add new test
case for strip-tangle.
* doc/org-manual.org (Noweb Reference Syntax): adjust documentation for
the noweb header argument.
* etc/ORG-NEWS: add entry for new header argument value.

This patch adds the "strip-tangle" option for the :noweb header
argument. This strips the noweb tags before tangling the block. This can
be useful for e.g. testing purposes where one wants to use a block as
test case that can be both run inline as well as tangled into a file for
automated testing.

Thanks, but it looks like your PGP signature corrupted the email patch.
Could you please create a separate patch file and attach it in the
reply?
Sure, here's the patch attached, as generated by "git format-patch 
origin/main".
From 0133162ef78e007fe4918016a4aabf8bc3734488 Mon Sep 17 00:00:00 2001
From: Daniel Ziltener 
Date: Sun, 30 Oct 2022 01:20:53 +0200
Subject: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle

* lisp/ob-tangle.el (org-babel-tangle-single-block): strip noweb tags
from block if :noweb has been set to strip-tangle.
* lisp/ob-core.el (org-babel-common-header-args-w-values): add
"strip-tangle" as new allowed value.
* testing/lisp/test-ob-tangle.el (ob-tangle/strip-tangle): add new test
case for strip-tangle.
* doc/org-manual.org (Noweb Reference Syntax): adjust documentation for
the noweb header argument.
* etc/ORG-NEWS: add entry for new header argument value.

This patch adds the "strip-tangle" option for the :noweb header
argument. This strips the noweb tags before tangling the block. This can
be useful for e.g. testing purposes where one wants to use a block as
test case that can be both run inline as well as tangled into a file for
automated testing.
---
 doc/org-manual.org |  6 ++
 etc/ORG-NEWS   |  5 +
 lisp/ob-core.el|  2 +-
 lisp/ob-tangle.el  |  4 +++-
 testing/lisp/test-ob-tangle.el | 26 ++
 5 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 18a050069..064d51bcd 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -18993,6 +18993,12 @@ tangled, or exported.
   Expansion of noweb syntax references in the body of the code block
   when tangling.  No expansion when evaluating or exporting.
 
+- =strip-tangle= ::
+
+  Expansion of noweb syntax references in the body of the code block
+  when evaluating or exporting.  Removes noweb syntax references
+  when exporting.
+
 - =no-export= ::
 
   Expansion of noweb syntax references in the body of the code block
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 6e875deb6..2c66d2e45 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -228,6 +228,11 @@ commands.
 =:noweb-prefix= can be set to =no= to prevent the prefix characters
 from being repeated when expanding a multiline noweb reference.
 
+*** New =:noweb= babel header argument value =strip-tangle=
+
+=:noweb= can be set to =strip-tangle= to strip the noweb syntax references
+before tangling.
+
 *** New LaTeX source block backend using =engraved-faces-latex=
 
 When ~org-latex-src-block-backend~ is set to ~engraved~,
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 518831ec6..c52f113b4 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -414,7 +414,7 @@ then run `org-babel-switch-to-session'."
 (mkdirp	. ((yes no)))
 (no-expand)
 (noeval)
-    (noweb	. ((yes no tangle no-export strip-export)))
+(noweb	. ((yes no tangle strip-tangle no-export strip-export)))
 (noweb-ref	. :any)
 (noweb-sep  . :any)
 (noweb-prefix . ((no yes)))
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 2da92efaf..d9d847195 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -536,7 +536,9 @@ non-nil, return the full association list to be used by
 	 (body
 	  ;; Run the tangle-body-hook.
   (let ((body (if (org-babel-noweb-p params :tangle)
-			  (org-babel-expand-noweb-references info)
+  (if (string= "strip-tangle" (cdr (assq :noweb (nth 2 info))))
+  (replace-regexp-in-string (org-babel-noweb-wrap) "" (nth 1 info))
+			(org-babel-expand-noweb-references info))
 			(nth 1 info
 	(with-temp-buffer
 	  (insert
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index a0003ee40..af2a72682 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -510,6 +510,32 @@ another block
 		(org-split-string (buffer-string
 	  (delete-fil

Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-29 Thread Timothy
Hi Daniel,

> Add a “strip-tangle” noweb option to strip the noweb tags when tangling, but
> keep and expand them otherwise.

I must admit I can’t see the point of this — would you mind providing an
example of when this would be useful?

All the best,
Timothy

-- 
Timothy (‘tecosaur’/‘TEC’), 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/tec>.


Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-29 Thread Ihor Radchenko
Daniel Ziltener  writes:

> From: Daniel Ziltener 
>
> * lisp/ob-tangle.el (org-babel-tangle-single-block): strip noweb tags
> from block if :noweb has been set to strip-tangle.
> * lisp/ob-core.el (org-babel-common-header-args-w-values): add
> "strip-tangle" as new allowed value.
> * testing/lisp/test-ob-tangle.el (ob-tangle/strip-tangle): add new test
> case for strip-tangle.
> * doc/org-manual.org (Noweb Reference Syntax): adjust documentation for
> the noweb header argument.
> * etc/ORG-NEWS: add entry for new header argument value.
>
> This patch adds the "strip-tangle" option for the :noweb header
> argument. This strips the noweb tags before tangling the block. This can
> be useful for e.g. testing purposes where one wants to use a block as
> test case that can be both run inline as well as tangled into a file for
> automated testing.

Thanks, but it looks like your PGP signature corrupted the email patch.
Could you please create a separate patch file and attach it in the
reply?

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



Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-29 Thread Daniel Ziltener

From: Daniel Ziltener 

* lisp/ob-tangle.el (org-babel-tangle-single-block): strip noweb tags
from block if :noweb has been set to strip-tangle.
* lisp/ob-core.el (org-babel-common-header-args-w-values): add
"strip-tangle" as new allowed value.
* testing/lisp/test-ob-tangle.el (ob-tangle/strip-tangle): add new test
case for strip-tangle.
* doc/org-manual.org (Noweb Reference Syntax): adjust documentation for
the noweb header argument.
* etc/ORG-NEWS: add entry for new header argument value.

This patch adds the "strip-tangle" option for the :noweb header
argument. This strips the noweb tags before tangling the block. This can
be useful for e.g. testing purposes where one wants to use a block as
test case that can be both run inline as well as tangled into a file for
automated testing.
---
 doc/org-manual.org |  6 ++
 etc/ORG-NEWS   |  5 +
 lisp/ob-core.el    |  2 +-
 lisp/ob-tangle.el  |  4 +++-
 testing/lisp/test-ob-tangle.el | 26 ++
 5 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 18a050069..064d51bcd 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -18993,6 +18993,12 @@ tangled, or exported.
   Expansion of noweb syntax references in the body of the code block
   when tangling.  No expansion when evaluating or exporting.

+- =strip-tangle= ::
+
+  Expansion of noweb syntax references in the body of the code block
+  when evaluating or exporting.  Removes noweb syntax references
+  when exporting.
+
 - =no-export= ::

   Expansion of noweb syntax references in the body of the code block
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 6e875deb6..2c66d2e45 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -228,6 +228,11 @@ commands.
 =:noweb-prefix= can be set to =no= to prevent the prefix characters
 from being repeated when expanding a multiline noweb reference.

+*** New =:noweb= babel header argument value =strip-tangle=
+
+=:noweb= can be set to =strip-tangle= to strip the noweb syntax references
+before tangling.
+
 *** New LaTeX source block backend using =engraved-faces-latex=

 When ~org-latex-src-block-backend~ is set to ~engraved~,
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 518831ec6..c52f113b4 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -414,7 +414,7 @@ then run `org-babel-switch-to-session'."
 (mkdirp    . ((yes no)))
 (no-expand)
 (noeval)
-    (noweb    . ((yes no tangle no-export strip-export)))
+    (noweb    . ((yes no tangle strip-tangle no-export strip-export)))
     (noweb-ref    . :any)
     (noweb-sep  . :any)
 (noweb-prefix . ((no yes)))
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 2da92efaf..d9d847195 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -536,7 +536,9 @@ non-nil, return the full association list to be used by
  (body
   ;; Run the tangle-body-hook.
   (let ((body (if (org-babel-noweb-p params :tangle)
-              (org-babel-expand-noweb-references info)
+  (if (string= "strip-tangle" (cdr (assq :noweb 
(nth 2 info
+  (replace-regexp-in-string 
(org-babel-noweb-wrap) "" (nth 1 info))

+                (org-babel-expand-noweb-references info))
         (nth 1 info
     (with-temp-buffer
   (insert
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index a0003ee40..af2a72682 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -510,6 +510,32 @@ another block
         (org-split-string (buffer-string
   (delete-file file))

+(ert-deftest ob-tangle/strip-tangle ()
+  "Test if strip-tangle works correctly when tangling noweb code blocks."
+  (should
+   (equal '("1")
+  (let ((file (make-temp-file "org-tangle-")))
+    (unwind-protect
+    (progn
+  (org-test-with-temp-text-in-file
+       (format "
+#+name: block1
+#+begin_src elisp
+2
+#+end_src
+
+#+begin_src elisp :noweb strip-tangle :tangle %s
++1<>
+#+end_src
+" file)
+   (let ((org-babel-noweb-error-all-langs nil)
+ (org-babel-noweb-error-langs nil))
+ (org-babel-tangle)))
+  (with-temp-buffer
+    (insert-file-contents file)
+    (org-split-string (buffer-string
+  (delete-file file))
+
 (ert-deftest ob-tangle/detangle-false-positive ()
   "Test handling of false positive link during detangle."
   (let (buffer)
--
2.35.3




OpenPGP_0x752C7F031AADF16F.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-28 Thread Ihor Radchenko
Daniel Ziltener  writes:

>> Could you please follow
>> https://orgmode.org/worg/org-contribute.html#commit-messages for the
>> commit log entries?
> I tried to follow that. To follow it "more" I guess I have to make one 
> commit per file I changed? There isn't an example for multiple files, so 
> I was not sure how exactly to follow it.

See https://orgmode.org/worg/org-contribute.html#org5bd92c9. Basically,
you need to provide changed function/variable/section name in the
changelog.

When you make the same change in multiple files, just do something like

* lisp/org-capture.el (org-capture-set-plist):
* doc/org.texi (Capture): .

>> Also, do you have FSF copyright assignment? If no, you also need to add
>> TINYCHANGE cookie to the commit message. See
>> https://orgmode.org/worg/org-contribute.html#first-patch
> I do, yes, I sent in the signed form on Wednesday as requested by the FSF.

Great!
They should reply within 5 working days. If they don't, you can followup
with them and wait another 5 days. If that also does not work, let us
know.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at .
Support Org development at ,
or support my work at 



Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-28 Thread Daniel Ziltener

Am 28.10.22 um 04:19 schrieb Ihor Radchenko:

dzilte...@lyrion.ch writes:


From: Daniel Ziltener 

* ob-tangle.el, ob-core.el, test-ob-tangle.el, org-manual.org: Add a
"strip-tangle" noweb option to strip the noweb tags when tangling, but
keep and expand them otherwise.

Thanks for the patch!

If I understand correctly, you are suggesting

Could you please follow
https://orgmode.org/worg/org-contribute.html#commit-messages for the
commit log entries?
I tried to follow that. To follow it "more" I guess I have to make one 
commit per file I changed? There isn't an example for multiple files, so 
I was not sure how exactly to follow it.

Also, do you have FSF copyright assignment? If no, you also need to add
TINYCHANGE cookie to the commit message. See
https://orgmode.org/worg/org-contribute.html#first-patch

I do, yes, I sent in the signed form on Wednesday as requested by the FSF.

+- =strip-tangle= ::
+
+  Expansion of noweb syntax references in the body of the code block
+  when evaluating or exporting. Removes noweb syntax references
+  when tangling.

This is a new feature and thus should also be mentioned in etc/NEWS.

Also, please use double space between sentences. See
doc/Documentation_Standards.org.

Noted, I'll do both of these.

(let ((allowed-values (cl-case context
- (:tangle '("yes" "tangle" "no-export" "strip-export"))
- (:eval   '("yes" "no-export" "strip-export" "eval"))
- (:export '("yes")
+ (:tangle '("yes" "tangle" "no-export" "strip-export" 
"strip-tangle"))
+     (:eval   '("yes" "no-export" "strip-export" "eval" 
"strip-tangle"))
+ (:export '("yes" "strip-tangle")

AFAIU, you are suggesting a new value for :noweb header argument.
But this function has nothing to do with :noweb. This change will check
for :tangle strip-tangle, :eval strip-tangle, and :export strip-tangle.
What is the purpose?
I suppose then I made a mistake there. My intention was what you mention 
next:

Also, the allowed values of standard header args are defined in
org-babel-common-header-args-w-values, which you did not change.

I will adjust that one instead.


OpenPGP_0x752C7F031AADF16F.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature


Re: [PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-27 Thread Ihor Radchenko
dzilte...@lyrion.ch writes:

> From: Daniel Ziltener 
>
> * ob-tangle.el, ob-core.el, test-ob-tangle.el, org-manual.org: Add a
> "strip-tangle" noweb option to strip the noweb tags when tangling, but
> keep and expand them otherwise.

Thanks for the patch!

If I understand correctly, you are suggesting 

Could you please follow
https://orgmode.org/worg/org-contribute.html#commit-messages for the
commit log entries?

Also, do you have FSF copyright assignment? If no, you also need to add
TINYCHANGE cookie to the commit message. See
https://orgmode.org/worg/org-contribute.html#first-patch

> +- =strip-tangle= ::
> +
> +  Expansion of noweb syntax references in the body of the code block
> +  when evaluating or exporting. Removes noweb syntax references
> +  when tangling.

This is a new feature and thus should also be mentioned in etc/NEWS.

Also, please use double space between sentences. See
doc/Documentation_Standards.org.

>(let ((allowed-values (cl-case context
> -   (:tangle '("yes" "tangle" "no-export" "strip-export"))
> -   (:eval   '("yes" "no-export" "strip-export" "eval"))
> -   (:export '("yes")
> +   (:tangle '("yes" "tangle" "no-export" "strip-export" 
> "strip-tangle"))
> +   (:eval   '("yes" "no-export" "strip-export" "eval" 
> "strip-tangle"))
> +   (:export '("yes" "strip-tangle")

AFAIU, you are suggesting a new value for :noweb header argument.
But this function has nothing to do with :noweb. This change will check
for :tangle strip-tangle, :eval strip-tangle, and :export strip-tangle.
What is the purpose?

Also, the allowed values of standard header args are defined in
org-babel-common-header-args-w-values, which you did not change.

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



[PATCH] lisp/ob-tangle.el, lisp/ob-core.el: Add strip-tangle noweb option

2022-10-26 Thread dziltener
From: Daniel Ziltener 

* ob-tangle.el, ob-core.el, test-ob-tangle.el, org-manual.org: Add a
"strip-tangle" noweb option to strip the noweb tags when tangling, but
keep and expand them otherwise.
---
 doc/org-manual.org |  6 ++
 lisp/ob-core.el|  6 +++---
 lisp/ob-tangle.el  |  4 +++-
 testing/lisp/test-ob-tangle.el | 27 +++
 4 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 18a050069..cb85f4702 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -18993,6 +18993,12 @@ tangled, or exported.
   Expansion of noweb syntax references in the body of the code block
   when tangling.  No expansion when evaluating or exporting.
 
+- =strip-tangle= ::
+
+  Expansion of noweb syntax references in the body of the code block
+  when evaluating or exporting. Removes noweb syntax references
+  when tangling.
+
 - =no-export= ::
 
   Expansion of noweb syntax references in the body of the code block
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 518831ec6..ae77182ef 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2880,9 +2880,9 @@ parameters when merging lists."
   "Check if PARAMS require expansion in CONTEXT.
 CONTEXT may be one of :tangle, :export or :eval."
   (let ((allowed-values (cl-case context
- (:tangle '("yes" "tangle" "no-export" "strip-export"))
- (:eval   '("yes" "no-export" "strip-export" "eval"))
- (:export '("yes")
+ (:tangle '("yes" "tangle" "no-export" "strip-export" 
"strip-tangle"))
+ (:eval   '("yes" "no-export" "strip-export" "eval" 
"strip-tangle"))
+ (:export '("yes" "strip-tangle")
 (cl-some (lambda (v) (member v allowed-values))
 (split-string (or (cdr (assq :noweb params)) "")
 
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 2da92efaf..d9d847195 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -536,7 +536,9 @@ non-nil, return the full association list to be used by
 (body
  ;; Run the tangle-body-hook.
   (let ((body (if (org-babel-noweb-p params :tangle)
- (org-babel-expand-noweb-references info)
+  (if (string= "strip-tangle" (cdr (assq :noweb (nth 2 
info
+  (replace-regexp-in-string (org-babel-noweb-wrap) 
"" (nth 1 info))
+   (org-babel-expand-noweb-references info))
(nth 1 info
(with-temp-buffer
  (insert
diff --git a/testing/lisp/test-ob-tangle.el b/testing/lisp/test-ob-tangle.el
index a0003ee40..455745e5c 100644
--- a/testing/lisp/test-ob-tangle.el
+++ b/testing/lisp/test-ob-tangle.el
@@ -510,6 +510,33 @@ another block
(org-split-string (buffer-string
  (delete-file file))
 
+(ert-deftest ob-tangle/strip-tangle ()
+  "Test if strip-tangle works correctly when tangling noweb code blocks."
+  (should
+   (equal '("1")
+  (let ((file (make-temp-file "org-tangle-")))
+(unwind-protect
+(progn
+      (org-test-with-temp-text-in-file
+   (format "
+#+name: block1
+#+begin_src elisp
+2
+#+end_src
+
+#+begin_src elisp :noweb strip-tangle :tangle %s
+1<>
+#+end_src
+"
+   file)
+   (let ((org-babel-noweb-error-all-langs nil)
+ (org-babel-noweb-error-langs nil))
+ (org-babel-tangle)))
+  (with-temp-buffer
+(insert-file-contents file)
+(org-split-string (buffer-string
+  (delete-file file))
+
 (ert-deftest ob-tangle/detangle-false-positive ()
   "Test handling of false positive link during detangle."
   (let (buffer)
-- 
2.35.3




Re: Org babel noweb expansion includes extra newline

2022-10-23 Thread Ihor Radchenko
pareto optimal  writes:

> #+RESULTS:
> #+begin_example
> "git ls-files ~/system/users/profiles/emacs/default.nix
> git ls-files ~/system/users/profiles/emacs/emacs-packages.nix
> git ls-files ~/system/users/profiles/chat/default.nix
> git ls-files ~/system/profiles/misc/default.nix
> git ls-files "
> #+end_example
> *** here's what hapepns if we just run =all-tangled-filepaths=
>
> ...
> #+name: files-to-remove-from-git
> #+begin_src sh :noweb yes :dir ~/system
> git ls-files <>
> #+end_src

This is because noweb automatically appends text before <<...>> to each
line of multi-line noweb expansions.
See https://orgmode.org/manual/Noweb-Reference-Syntax.html

On the latest Org, this behaviour can be disabled using :noweb-prefix no
header 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>



Re: [O] Quoting of noweb references and variable noweb function arguments

2022-10-19 Thread Ihor Radchenko
Cody Goodman  writes:

> I'm having an issue with quoting noweb functions. Since quote is used to
> signify a variable being passed into the noweb syntax function call I
> cannot put a quote around theno web function call and end up with invalid
> bash.
>#+begin_src sh :noweb yes :var logValUserVar="user2"
> logValUserPass="pass2" jqExpr="[]keys"
>json=<>
>echo $json | jq $jqExpr
>#+end_src

You cannot pass src block variables to noweb. It is currently not supported.

And you cannot pass bash bindings to noweb. Noweb expansion happens
before executing the bash code. It is not possible to know that bash
assignments at that point.

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



[BUG?] :eval strip-export acts like :eval yes :noweb yes even during export (was: :eval yes use and documentation)

2022-10-07 Thread Ihor Radchenko
Ihor Radchenko  writes:

> Charles Millar  writes:
>
>> Is ":eval yes" officially allowed and, if so, why is it not documented?
>>
>> Should it be?
>
> Yes, it should be. Moreover, we have strip-export value undocumented.
>
> Confirmed.

Fixed on main.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=1966a7a8a8a2934443f24ca9c968a4eba09c3650

I documented ":eval yes".

strip-export value is actually used, but I feel that it is completely
wrong.

`org-babel-common-header-args-w-values' lists "strip-export" as an
allowed value of :eval argument. Further, `org-babel-noweb-p' returns
non-nil for ":eval strip-export".

The consequences for the callers of `org-babel-noweb-p' when ":eval
strip-export" are the following:

1. `org-babel-expand-noweb-references', `org-babel-execute-src-block',
   and `org-babel-expand-src-block' always expand noweb references.
2. `org-babel-lob-ingest' always expands noweb references.
3. `org-babel-exp-results' __always__ expands noweb references.

The (3) is certainly against intuition. `org-babel-exp-results' (which
is a part of export process) __does expand noweb references__ when
:eval is strip-export.

Not to mention that "strip-export" has nothing to do with evaluation
itself. Evaluation cannot be "stripped".

Should we remove :eval strip-export altogether? It is confusing and does
not yield expected results. Since it is also not documented, we should
not worry about breaking user configs.

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



Re: [O] Plantuml w/ noweb and cached results

2022-09-23 Thread Ken Mankoff
Hi Ihor,

Thank you for your reply. I apologize for not doing a more thorough test with 
'-Q'. I'll try to find the problem in my setup.

  -k.

On 2022-09-22 at 22:02 -04, Ihor Radchenko  wrote:
> Ken Mankoff  writes:
>
>> #+BEGIN_SRC plantuml :noweb yes :file cache-yes.png
>> @startjson
>> <>
>> @endjson
>> #+END_SRC
>>
>> #+RESULTS:
>> [[file:cache-yes.png]]
>>
>> The above graphic is an error message. It states,
>>
>> "Your data does not sound like JSON data".
>
> I tried your example it is working just fine on my side.
> I suggest you to try reproducing starting from Emacs -Q.
> See https://orgmode.org/manual/Feedback.html. You may also find
> https://github.com/alphapapa/with-emacs.sh useful to install packages
> into a temporary clean Emacs instance.




Re: [O] Plantuml w/ noweb and cached results

2022-09-22 Thread Ihor Radchenko
Ken Mankoff  writes:

> #+BEGIN_SRC plantuml :noweb yes :file cache-yes.png
> @startjson
> <>
> @endjson
> #+END_SRC
>
> #+RESULTS:
> [[file:cache-yes.png]]
>
> The above graphic is an error message. It states,
>
> "Your data does not sound like JSON data".

I tried your example it is working just fine on my side.
I suggest you to try reproducing starting from Emacs -Q.
See https://orgmode.org/manual/Feedback.html. You may also find
https://github.com/alphapapa/with-emacs.sh useful to install packages
into a temporary clean Emacs instance.

-- 
Ihor Radchenko,
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



[O] Plantuml w/ noweb and cached results

2022-09-21 Thread Ken Mankoff
Hello,

I'm not sure if this is a bug in ob-restclient, ob-plantuml, or Babel. If 
someone can help me figure out where, I can move this report to GitHub (if 
ob-restclient) - otherwise I think it remains here.

I cannot use ob-plantuml if results are cached. See example:

#+NAME: cache-no
#+BEGIN_SRC restclient :jq .[0].address :cache no

GET https://jsonplaceholder.typicode.com/users
#+END_SRC

#+RESULTS: cache-no
#+BEGIN_SRC js

{
  "street": "Kulas Light",
  "suite": "Apt. 556",
  "city": "Gwenborough",
  "zipcode": "92998-3874",
  "geo": {
"lat": "-37.3159",
"lng": "81.1496"
  }
}
#+END_SRC

#+BEGIN_SRC plantuml :noweb yes :file cache-no.png
@startjson
<>
@endjson
#+END_SRC


#+RESULTS:
[[file:cache-no.png]]

The above graphic is generated by plantuml.



#+NAME: cache-yes
#+BEGIN_SRC restclient :jq .[0].address :cache yes

GET https://jsonplaceholder.typicode.com/users
#+END_SRC

#+RESULTS[(2022-09-20 15:52:59) c41e0371fd392d6fbfd07ff4078abf8c387885ea]: 
cache-yes

#+BEGIN_SRC js
{
  "street": "Kulas Light",
  "suite": "Apt. 556",
  "city": "Gwenborough",
  "zipcode": "92998-3874",
  "geo": {
"lat": "-37.3159",
"lng": "81.1496"
  }
}
#+END_SRC

#+BEGIN_SRC plantuml :noweb yes :file cache-yes.png
@startjson
<>
@endjson
#+END_SRC

#+RESULTS:
[[file:cache-yes.png]]

The above graphic is an error message. It states,

"Your data does not sound like JSON data".

  -k.

n



Re: [PATCH] Make :var foo=name-of-src-block assign the source block code instead of currently assigned result of evaluation (was: [PATCH] Add :noweb-prefix and :noweb-trans babel header arguments)

2022-08-30 Thread Ihor Radchenko
Sébastien Miquel  writes:

> I've implemented this proposal in the patch attached.
>
> Does it look good to you ?

Thanks! The patch looks mostly fine.

Applied onto main via 72f66ca0b.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=72f66ca0b9d336e0da61b17cbe8ce183eef364dd

A small side effect of the patch is that name[] will also work for
lists:

#+name: test
- one
- two
- three


#+begin_src emacs-lisp :var x=test[]
  (message "%S" x)
#+end_src

#+RESULTS:
: (("one") ("two") ("three"))

which is not intentional, but does not break anything and also somewhat
logical.

#+begin_src emacs-lisp :var x=test[1:2]
  (message "%S" x)
#+end_src

#+RESULTS:
: (("two") ("three"))

worked in the past anyway, despite not being documented.

-- 
Ihor Radchenko,
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



Re: [PATCH] Make :var foo=name-of-src-block assign the source block code instead of currently assigned result of evaluation (was: [PATCH] Add :noweb-prefix and :noweb-trans babel header arguments)

2022-08-29 Thread Sébastien Miquel

Hi Ihor,

I've implemented this proposal in the patch attached.

Does it look good to you ?

--
Sébastien MiquelFrom b1b783dc80821b07937ac4211ec28df8726fff1c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= 
Date: Sat, 13 Aug 2022 20:49:27 +0200
Subject: [PATCH] New babel syntax to pass src block contents as argument

* lisp/ob-ref.el (org-babel-ref-resolve): Add support for
`named-block[]' syntax, resolving to the contents of a named-block.
* lisp/ob-core.el (org-babel-read-element): Read a code block into its
contents, like other blocks.
* testing/listp/test-ob.el (test-ob/block-content-resolution): Test
block content resolution.
* doc/org-manual.org: Document syntax.
* etc/ORG-NEWS: Document syntax.
---
 doc/org-manual.org  |  7 ---
 etc/ORG-NEWS|  5 +
 lisp/ob-core.el |  2 +-
 lisp/ob-ref.el  | 10 ++
 testing/lisp/test-ob.el | 15 +++
 5 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/doc/org-manual.org b/doc/org-manual.org
index 57a57a6fe..794682b49 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -17505,9 +17505,10 @@ a colon, for example: =:var table=other-file.org:example-table=.
   : 4
   #+end_example
 
-- literal example ::
+- literal example, or code block contents ::
 
-  A literal example block named with a =NAME= keyword.
+  A code block or literal example block named with a =NAME= keyword,
+  followed by brackets (optional for example blocks).
 
   #+begin_example
   ,#+NAME: literal-example
@@ -17517,7 +17518,7 @@ a colon, for example: =:var table=other-file.org:example-table=.
   ,#+END_EXAMPLE
 
   ,#+NAME: read-literal-example
-  ,#+BEGIN_SRC emacs-lisp :var x=literal-example
+  ,#+BEGIN_SRC emacs-lisp :var x=literal-example[]
 (concatenate #'string x " for you.")
   ,#+END_SRC
 
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 7dae03dc6..d6d99a64b 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -288,6 +288,11 @@ The =org-md-toplevel-hlevel= customization variable sets the heading
 level used for top level headings, much like how
 =org-html-toplevel-hlevel= sets the heading level used for top level
 headings in HTML export.
+*** Babel: new syntax to pass the contents of a src block as argument
+
+Use the header argument =:var x=code-block[]= or
+: #+CALL: fn(x=code-block[])
+to pass the contents of a named code block as a string argument.
 
 ** New options
 *** A new custom setting =org-hide-drawer-startup= to control initial folding state of drawers
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 68dd5557c..c52ef9ed6 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2156,7 +2156,7 @@ Return nil if ELEMENT cannot be read."
 	(or (org-babel--string-to-number v) v)))
  (`table (org-babel-read-table))
  (`plain-list (org-babel-read-list))
- (`example-block
+ ((or `example-block `src-block)
   (let ((v (org-element-property :value element)))
 	(if (or org-src-preserve-indentation
 		(org-element-property :preserve-indent element))
diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el
index 87a7ccf63..ee2745e09 100644
--- a/lisp/ob-ref.el
+++ b/lisp/ob-ref.el
@@ -124,12 +124,14 @@ Emacs Lisp representation of the value of the variable."
   (save-excursion
 	(let ((case-fold-search t)
 	  args new-refere new-header-args new-referent split-file split-ref
-	  index)
+	  index contents)
 	  ;; if ref is indexed grab the indices -- beware nested indices
-	  (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref)
+	  (when (and (string-match "\\[\\([^\\[]*\\)\\]$" ref)
 		 (let ((str (substring ref 0 (match-beginning 0
 		   (= (cl-count ?\( str) (cl-count ?\) str
-	(setq index (match-string 1 ref))
+(if (> (length (match-string 1 ref)) 0)
+	(setq index (match-string 1 ref))
+  (setq contents t))
 	(setq ref (substring ref 0 (match-beginning 0
 	  ;; assign any arguments to pass to source block
 	  (when (string-match
@@ -171,7 +173,7 @@ Emacs Lisp representation of the value of the variable."
 (throw :found
    (org-babel-execute-src-block
 	nil (org-babel-lob-get-info e) params)))
-			   (`src-block
+			   ((and `src-block (guard (not contents)))
 (throw :found
    (org-babel-execute-src-block
 	nil nil
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index a62bd56bf..c944ccd39 100644
--- a/testing/lisp/test-ob.el
+++ b/testing/lisp/test-ob.el
@@ -178,6 +178,21 @@ should still return the link."
 			(point-at-bol)
 			(point-at-eol))
 
+(ert-deftest test-ob/block-content-resolution ()
+  "Test block content resolution."
+  (org-test-with-temp-text-in-file "
+
+#+name: four
+#+begin_src emacs-lisp
+  (list 1 2 3 4)
+#+end_src
+
+#+begin_src emacs-lisp :var four=four[]
+  (length (eval (car (read-from-string four
+#+end_src"
+   (org-babel-next-src-block 2)
+   (should (=

Re: [PATCH] org-babel: Do not echo output of resolved noweb references

2022-08-22 Thread Ihor Radchenko
Ihor Radchenko  writes:

> Currently, every time Org resolves noweb references, the behaviour is
> resembling :results silent. That is, the results of evaluation are
> displayed, but not inserted after the executed source blocks.
>
> I propose to change this by using :results none that will display
> "results silenced" echo instead of messaging the whole result string.

Applied onto main via 2dfdc8953.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=2dfdc895351dd867042a4693a63cc688a7be1fb5

-- 
Ihor Radchenko,
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



Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-13 Thread Ihor Radchenko
Max Nikulin  writes:

>>> It looks like (string-prefix-p "file:" bare) but without the complex
>>> regexp. I see, such code was used before. By the way, why it is "bare",
>>> not e.g. "target"?
>> 
>> match-end is used later in the code. Thus, string-match is justified.
>
> The matched string is always "file:", so there is no actual point in 
> `string-match'. It does not really matter however.
>
> (let ((params '((:tangle . "/home/user/file.el")))
>(type "file:")
>(target "file:///dev/null"))
>(if (and target
>  org-babel-tangle-use-relative-file-links
>  (string-prefix-p type target))
>(concat type
> (file-relative-name
> (substring target (length type))
>  (file-name-directory (alist-get :tangle params
>  target))
>
> In the code existed before and moved by the committed patch I do not 
> like that "then" branch of the `if' form uses side effect of evaluation 
> of the condition.

Sounds reasonable.
Could you make a patch?

-- 
Ihor Radchenko,
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



Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-13 Thread Max Nikulin

On 13/08/2022 13:42, Ihor Radchenko wrote:

Max Nikulin writes:


+ (bare (and (string-match org-link-bracket-re l)
+(match-string 1 l
+(when bare
+  (if (and org-babel-tangle-use-relative-file-links
+   (string-match org-link-types-re bare)
+   (string= (match-string 1 bare) "file"))


It looks like (string-prefix-p "file:" bare) but without the complex
regexp. I see, such code was used before. By the way, why it is "bare",
not e.g. "target"?


match-end is used later in the code. Thus, string-match is justified.


The matched string is always "file:", so there is no actual point in 
`string-match'. It does not really matter however.


(let ((params '((:tangle . "/home/user/file.el")))
  (type "file:")
  (target "file:///dev/null"))
  (if (and target
   org-babel-tangle-use-relative-file-links
   (string-prefix-p type target))
  (concat type
  (file-relative-name
   (substring target (length type))
   (file-name-directory (alist-get :tangle params
target))

In the code existed before and moved by the committed patch I do not 
like that "then" branch of the `if' form uses side effect of evaluation 
of the condition.





Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-12 Thread Ihor Radchenko
Max Nikulin  writes:

>> + (bare (and (string-match org-link-bracket-re l)
>> +(match-string 1 l
>> +(when bare
>> +  (if (and org-babel-tangle-use-relative-file-links
>> +   (string-match org-link-types-re bare)
>> +   (string= (match-string 1 bare) "file"))
>
> It looks like (string-prefix-p "file:" bare) but without the complex 
> regexp. I see, such code was used before. By the way, why it is "bare", 
> not e.g. "target"?

match-end is used later in the code. Thus, string-match is justified.

-- 
Ihor Radchenko,
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



Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-12 Thread Ihor Radchenko
Hraban Luyat  writes:

>>> @Ihor: I have rebased the patch and attached it.

Applied onto main via 8a781d35d.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=8a781d35dc68f20fa2a5546c98ba3d9b77ee3cda

Thanks again for your contribution!


-- 
Ihor Radchenko,
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



Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-12 Thread Max Nikulin

On 12/08/2022 09:21, Hraban Luyat wrote:


@Max: what do you think of when-let? That seems more appropriate for
this situation. Thoughts?


At first I thought about compatibility, but this form is available since 
Emacs-25, so it is not a problem.


Maybe I missed something, but I do not see clear advantage. Anyway it 
leads to `if' nested inside `when-let'.



+ (bare (and (string-match org-link-bracket-re l)
+(match-string 1 l
+(when bare
+  (if (and org-babel-tangle-use-relative-file-links
+   (string-match org-link-types-re bare)
+   (string= (match-string 1 bare) "file"))


It looks like (string-prefix-p "file:" bare) but without the complex 
regexp. I see, such code was used before. By the way, why it is "bare", 
not e.g. "target"?



+  (concat "file:"
+  (file-relative-name (substring bare (match-end 0))
+  (file-name-directory
+   (cdr (assq :tangle params)


I do not insist on changes since possible improvements are not really 
significant.





Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-11 Thread Hraban Luyat


On 8/11/22 12:26 AM, Ihor Radchenko wrote:
> Hraban Luyat  writes:
>
>>> Is there any problem with the following?
>>>
>>> (alist-get :tangle params)
>>
>> This bit of code was moved, I didn't write it. The original code uses a
>> variable `src-tfile' which isn't available here, so I reused the
>> definition of that variable (which is (cdr (assq yada yada))). When
>> creating this patch, I tried to change as little as possible, to keep
>> everything the same as much as I can. Don't write new code, just move
>> existing code around.
>>
>> The (cdr (assq ..)) is used in some other places, too; maybe it's worth
>> a separate refactor if we want to change that? I'd rather keep this
>> patch as isolated as possible.
>
> I suspect that alist-get was not there in Emacs 24.
> Otherwise, alist-get with no optional parameters is just a wrapper for
> (cdr (assq...))
>
> We can change it, though I do not see this as a big problem.
>
>> @Ihor: I have rebased the patch and attached it.
>
> Sorry, but the patch still does not apply on my side onto the current
> main branch.

Just rebased and recreated it. Based off
6acc58c9c6bcfd45dcc5964cac7e3df8347121cc.

@Max: what do you think of when-let? That seems more appropriate for
this situation. Thoughts?

>
> --
> Ihor Radchenko,
> 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/yantar92From 0c89c48a80b0095c40a1c4c478fdfd581e0110fd Mon Sep 17 00:00:00 2001
From: Hraban Luyat 
Date: Mon, 8 Aug 2022 16:58:05 -0400
Subject: [PATCH] =?UTF-8?q?ob-tangle.el:=20fix=20=E2=80=98:comments=20nowe?=
 =?UTF-8?q?b=E2=80=99=20double=20linking?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/ob-tangle.el: Refactor the double implementation to a single
helper function.  This avoids the double link wrapping.

* testing/lisp/test-ob-tangle.el: Add unit tests.

Babel tangle allows inserting comments at the tangled site which link
back to the source in the org file.  This linking was implemented
twice, to handle separate cases, but when using ‘:comments noweb’ it
ended up going through both codepaths.  This resulted in doubly
wrapped links.

By refactoring all link generation into a single function, this double
wrapping is avoided.

Example file, /tmp/test.org:

* Inner
#+name: inner
#+begin_src emacs-lisp
2
#+end_src

* Main
#+header: :tangle test.el :comments noweb :noweb yes
#+begin_src emacs-lisp
1
<>
#+end_src

Before:

;; [[file:test.org::*Main][Main:1]]
1
;; file:/tmp/test.org::inner][inner]]][inner]]
2
;; inner ends here
;; Main:1 ends here

After:

;; [[file:test.org::*Main][Main:1]]
1
;; [[file:test.org::inner][inner]]
2
;; inner ends here
;; Main:1 ends here
---
 lisp/ob-tangle.el  | 62 +-
 testing/lisp/test-ob-tangle.el | 56 ++
 2 files changed, 87 insertions(+), 31 deletions(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 4b8fad6ce..4db0adda7 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -469,6 +469,33 @@ code blocks by target file."
 (mapcar (lambda (b) (cons (car b) (nreverse (cdr b
(nreverse blocks
 
+(defun org-babel-tangle--unbracketed-link (params)
+  "Get a raw link to the src block at point, without brackets.
+
+The PARAMS are the 3rd element of the info for the same src block."
+  (unless (string= "no" (cdr (assq :comments params)))
+(save-match-data
+  (let* (;; The created link is transient.  Using ID is not necessary,
+ ;; but could have side-effects if used.  An ID property may
+ ;; be added to existing entries thus creating unexpected file
+ ;; modifications.
+ (org-id-link-to-org-use-id nil)
+ (l (org-no-properties
+ (cl-letf (((symbol-function 'org-store-link-functions)
+(lambda () nil)))
+   (org-store-link nil
+ (bare (and (string-match org-link-bracket-re l)
+(match-string 1 l
+(when bare
+  (if (and org-babel-tangle-use-relative-file-links
+   (string-match org-link-types-re bare)
+   (string= (match-string 1 bare) "file"))
+  (concat "file:"
+  (file-relative-name (substring bare (match-end 0))
+  (file-name-directory
+   (cdr (assq :tangle params)
+bare))
+
 (defun org

Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-11 Thread Max Nikulin

On 11/08/2022 03:54, Hraban Luyat wrote:

On 8/3/22 11:55 AM, Max Nikulin wrote:

Do you mean to rewrite

  (when bare (if x y bare))

to this?

  (and bare x y)

If that's what you meant, I think it would evaluate differently if bare
= truthy and x = falsy, right? Form 1 evaluates to `bare', form 2
evaluates to x (i.e. NIL). Or did I misunderstand the suggestion?


You are right. I tried to suggest an expression that was wrong.

I am going to make another attempt:

  (if (and bare
   org-babel-tangle-use-relative-file-links
   (string-match org-link-types-re bare)
   (string= (match-string 1 bare) "file"))
  (concat "file:"
  (file-relative-name (substring bare (match-end 0))
  (file-name-directory
   (cdr (assq :tangle params)
bare)

I do not think that such code is dramatically clearer, but at least it 
has 1 conditional form less.






Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-10 Thread Ihor Radchenko
Hraban Luyat  writes:

>> Is there any problem with the following?
>>
>>(alist-get :tangle params)
>
> This bit of code was moved, I didn't write it. The original code uses a
> variable `src-tfile' which isn't available here, so I reused the
> definition of that variable (which is (cdr (assq yada yada))). When
> creating this patch, I tried to change as little as possible, to keep
> everything the same as much as I can. Don't write new code, just move
> existing code around.
>
> The (cdr (assq ..)) is used in some other places, too; maybe it's worth
> a separate refactor if we want to change that? I'd rather keep this
> patch as isolated as possible.

I suspect that alist-get was not there in Emacs 24.
Otherwise, alist-get with no optional parameters is just a wrapper for
(cdr (assq...))

We can change it, though I do not see this as a big problem.

> @Ihor: I have rebased the patch and attached it.

Sorry, but the patch still does not apply on my side onto the current
main branch.

-- 
Ihor Radchenko,
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



Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-10 Thread Hraban Luyat


On 8/3/22 11:55 AM, Max Nikulin wrote:
>> +  (when bare
>> +(if (and org-babel-tangle-use-relative-file-links
>> + (string-match org-link-types-re bare)
>> + (string= (match-string 1 bare) "file"))
>> +(concat "file:"
>> +(file-relative-name (substring bare (match-end 0))
>> +(file-name-directory
>> + (cdr (assq :tangle params)
>
> Is there any problem with the following?
>
>(alist-get :tangle params)

This bit of code was moved, I didn't write it. The original code uses a
variable `src-tfile' which isn't available here, so I reused the
definition of that variable (which is (cdr (assq yada yada))). When
creating this patch, I tried to change as little as possible, to keep
everything the same as much as I can. Don't write new code, just move
existing code around.

The (cdr (assq ..)) is used in some other places, too; maybe it's worth
a separate refactor if we want to change that? I'd rather keep this
patch as isolated as possible.

>> +  bare)
>
> I have not read the patch care carefully, so I may miss something. It
> seems that (when bare (if (and other...) (action) bare)) may be
> simplified to
>
>   (and bare other... (action))
>
>

Do you mean to rewrite

 (when bare (if x y bare))

to this?

 (and bare x y)

If that's what you meant, I think it would evaluate differently if bare
= truthy and x = falsy, right? Form 1 evaluates to `bare', form 2
evaluates to x (i.e. NIL). Or did I misunderstand the suggestion?

@Ihor: I have rebased the patch and attached it.From 778558a5b0d38ee79d47b0068f68c761326e5e61 Mon Sep 17 00:00:00 2001
From: Hraban Luyat 
Date: Thu, 28 Jul 2022 22:32:08 -0400
Subject: [PATCH] =?UTF-8?q?ob-tangle.el:=20fix=20=E2=80=98:comments=20nowe?=
 =?UTF-8?q?b=E2=80=99=20double=20linking?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/ob-tangle.el: Refactor the double implementation to a single
helper function.  This avoids the double link wrapping.

* testing/lisp/test-ob-tangle.el: Add unit tests.

Babel tangle allows inserting comments at the tangled site which link
back to the source in the org file.  This linking was implemented
twice, to handle separate cases, but when using ‘:comments noweb’ it
ended up going through both codepaths.  This resulted in doubly
wrapped links.

By refactoring all link generation into a single function, this double
wrapping is avoided.

Example file, /tmp/test.org:

* Inner
#+name: inner
#+begin_src emacs-lisp
2
#+end_src

* Main
#+header: :tangle test.el :comments noweb :noweb yes
#+begin_src emacs-lisp
1
<>
#+end_src

Before:

;; [[file:test.org::*Main][Main:1]]
1
;; file:/tmp/test.org::inner][inner]]][inner]]
2
;; inner ends here
;; Main:1 ends here

After:

;; [[file:test.org::*Main][Main:1]]
1
;; [[file:test.org::inner][inner]]
2
;; inner ends here
;; Main:1 ends here
---
 lisp/ob-tangle.el  | 54 
 testing/lisp/test-ob-tangle.el | 56 ++
 2 files changed, 83 insertions(+), 27 deletions(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index fdba72278..f85f07e70 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -469,6 +469,29 @@ code blocks by target file."
 (mapcar (lambda (b) (cons (car b) (nreverse (cdr b
(nreverse blocks
 
+(defun org-babel-tangle--unbracketed-link (params)
+  "Get a raw link to the src block at point, without brackets.
+
+The PARAMS are the 3rd element of the info for the same src block."
+  (save-match-data
+(let* (;; The created link is transient.  Using ID is not necessary,
+   ;; but could have side-effects if used.  An ID property may
+   ;; be added to existing entries thus creating unexpected file
+   ;; modifications.
+   (org-id-link-to-org-use-id nil)
+   (l (org-no-properties (org-store-link nil)))
+   (bare (and (string-match org-link-bracket-re l)
+  (match-string 1 l
+  (when bare
+(if (and org-babel-tangle-use-relative-file-links
+ (string-match org-link-types-re bare)
+ (string= (match-string 1 bare) "file"))
+(concat "file:"
+(file-relative-name (substring bare (match-end 0))
+(file-name-directory
+ (cdr (assq :tangle params)
+  bare)
+
 (defun org-babel-tangle-single-block (block-counter &optional only-this-block)
   &qu

Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-03 Thread Max Nikulin

On 31/07/2022 06:42, Hraban Luyat wrote:

Before:

;; [[file:test.org::*Main][Main:1]]
1
;; file:/tmp/test.org::inner][inner]]][inner]]

After:

;; [[file:test.org::*Main][Main:1]]
1
;; [[file:test.org::inner][inner]]


Certainly the fix will be an improvement.


+  (when bare
+(if (and org-babel-tangle-use-relative-file-links
+ (string-match org-link-types-re bare)
+ (string= (match-string 1 bare) "file"))
+(concat "file:"
+(file-relative-name (substring bare (match-end 0))
+(file-name-directory
+ (cdr (assq :tangle params)


Is there any problem with the following?

 (alist-get :tangle params)


+  bare)


I have not read the patch care carefully, so I may miss something. It 
seems that (when bare (if (and other...) (action) bare)) may be 
simplified to


(and bare other... (action))




Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-03 Thread Ihor Radchenko
Hraban Luyat  writes:

> Done, new patch attached. Thanks for the feedback.

Thanks!
I have applied an earlier conflicting patch recently. Now, your patch
does not apply any more. Could you kindly update it?

Best,
Ihor



Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-08-03 Thread Bastien Guerry
Hi Ihor and Hraban,

Ihor Radchenko  writes:

> Bastien, could you please check the FSF records?

Yes, I confirm Hraban FSF assignment is all good.  I added him on 
https://orgmode.org/worg/contributors.html in the list of FSF-signed
contributors.

Thanks!

-- 
 Bastien



Re: [PATCH] ob-tangle.el: fix ‘:comments noweb’ double linking

2022-07-30 Thread Hraban Luyat
On 7/30/22 12:56 AM, Ihor Radchenko wrote:
> Hraban Luyat  writes:
>
>> This is my first org-mode patch, all comments welcome :). I signed a
>> copyright assignment to the FSF 2021-07-12.
>
> Thanks for the contribution!
>
> Bastien, could you please check the FSF records?
>
> The patch looks good in general. Few minor comments below.
>

Done, new patch attached. Thanks for the feedback.From 778558a5b0d38ee79d47b0068f68c761326e5e61 Mon Sep 17 00:00:00 2001
From: Hraban Luyat 
Date: Thu, 28 Jul 2022 22:32:08 -0400
Subject: [PATCH] =?UTF-8?q?ob-tangle.el:=20fix=20=E2=80=98:comments=20nowe?=
 =?UTF-8?q?b=E2=80=99=20double=20linking?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* lisp/ob-tangle.el: Refactor the double implementation to a single
helper function.  This avoids the double link wrapping.

* testing/lisp/test-ob-tangle.el: Add unit tests.

Babel tangle allows inserting comments at the tangled site which link
back to the source in the org file.  This linking was implemented
twice, to handle separate cases, but when using ‘:comments noweb’ it
ended up going through both codepaths.  This resulted in doubly
wrapped links.

By refactoring all link generation into a single function, this double
wrapping is avoided.

Example file, /tmp/test.org:

* Inner
#+name: inner
#+begin_src emacs-lisp
2
#+end_src

* Main
    #+header: :tangle test.el :comments noweb :noweb yes
#+begin_src emacs-lisp
1
<>
#+end_src

Before:

;; [[file:test.org::*Main][Main:1]]
1
;; file:/tmp/test.org::inner][inner]]][inner]]
2
;; inner ends here
;; Main:1 ends here

After:

;; [[file:test.org::*Main][Main:1]]
1
;; [[file:test.org::inner][inner]]
2
;; inner ends here
;; Main:1 ends here
---
 lisp/ob-tangle.el  | 54 
 testing/lisp/test-ob-tangle.el | 56 ++
 2 files changed, 83 insertions(+), 27 deletions(-)

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index fdba72278..f85f07e70 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -469,6 +469,29 @@ code blocks by target file."
 (mapcar (lambda (b) (cons (car b) (nreverse (cdr b
(nreverse blocks
 
+(defun org-babel-tangle--unbracketed-link (params)
+  "Get a raw link to the src block at point, without brackets.
+
+The PARAMS are the 3rd element of the info for the same src block."
+  (save-match-data
+(let* (;; The created link is transient.  Using ID is not necessary,
+   ;; but could have side-effects if used.  An ID property may
+   ;; be added to existing entries thus creating unexpected file
+   ;; modifications.
+   (org-id-link-to-org-use-id nil)
+   (l (org-no-properties (org-store-link nil)))
+   (bare (and (string-match org-link-bracket-re l)
+  (match-string 1 l
+  (when bare
+(if (and org-babel-tangle-use-relative-file-links
+ (string-match org-link-types-re bare)
+ (string= (match-string 1 bare) "file"))
+(concat "file:"
+(file-relative-name (substring bare (match-end 0))
+(file-name-directory
+ (cdr (assq :tangle params)
+  bare)
+
 (defun org-babel-tangle-single-block (block-counter &optional only-this-block)
   "Collect the tangled source for current block.
 Return the list of block attributes needed by
@@ -485,16 +508,7 @@ non-nil, return the full association list to be used by
 (extra (nth 3 info))
  (coderef (nth 6 info))
 (cref-regexp (org-src-coderef-regexp coderef))
-(link (let* (
-  ;; The created link is transient.  Using ID is
-  ;; not necessary, but could have side-effects if
-  ;; used.  An ID property may be added to
-  ;; existing entries thus creatin unexpected file
-  ;; modifications.
-  (org-id-link-to-org-use-id nil)
-  (l (org-no-properties (org-store-link nil
- (and (string-match org-link-bracket-re l)
-  (match-string 1 l
+(link (org-babel-tangle--unbracketed-link params))
 (source-name
  (or (nth 4 info)
  (format "%s:%d"
@@ -548,15 +562,7 @@ non-nil, return the full association list to be used by
(if org-babel-tangle-use-relative-file-links
(file-relative-name file)
  file)
-   (if (and org-babel-tangle-use-relative-file-links
-(string-match org-link-types-re link)
-(string= (match-stri

  1   2   3   4   5   6   7   >